1. sowmya235's Avatar
    We are facing problem in the below code while running the HTTP example[ we are not getting any response while running the below code]:Could anyone suggest us where we are going wrong?

    Mainactivity.java:
    public class MainActivity extends Activity {

    TextView httpStuff;
    private static String TAG = "Httpclient";

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "Inside main");
    httpStuff = (TextView) findViewById(R.id.tvHttp);
    //httpStuff.setText("Data returneedddd");

    HttpClient newClient = new HttpClient(this);
    newClient.sendHttpGetRequest("http://74.125.224.119");
    }

    HttpClient.java:
    public class HttpClient extends DefaultHttpClient {

    // Member variable
    private Activity mMainActivity;
    private HttpContext mHttpContext;
    private static String TAG = "Httpclient";

    /**
    * Constructor
    */
    HttpClient(Activity mainActivity) {

    // Initialize member variables
    mMainActivity = mainActivity;
    mHttpContext = new BasicHttpContext();
    }

    /**
    * Creates and send the Http get requests
    */
    public void sendHttpGetRequest(String hostUrl) {

    // Set the headers
    HttpGet httpGet = new HttpGet(hostUrl);
    httpGet.setHeader("Accept", "*/*");
    httpGet.setHeader("Accept-Encoding", "identity");
    httpGet.setHeader("X-Transcend-Version", "1");

    // Send the request Async
    HttpRequestAsync sendReqAsync = new HttpRequestAsync(mMainActivity, this, mHttpContext);
    sendReqAsync.execute(httpGet);
    }

    public void sendHttpGetRequest(HttpGet httpGet) {

    // Send the request Async to retrieve configuration file.
    HttpRequestAsync sendReqAsync = new HttpRequestAsync(mMainActivity, this, mHttpContext);
    sendReqAsync.execute(httpGet);
    }
    public void receivedResponse(HttpResponse response) {

    //TextView httpStuff = (TextView)mMainActivity.findViewById(R.id.tvHttp);

    if(response != null) {

    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    Log.d(TAG, response.getStatusLine().toString());
    //httpStuff.setText(response.getStatusLine().toString());
    } else {


    try {

    InputStream inputStream = response.getEntity().getContent();
    Reader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");

    // Read the data from the InputStream
    char[] buffer = new char[100];
    StringBuilder readData = new StringBuilder();
    int read;
    Log.d(TAG, "Got HTTP Response");
    Log.d(TAG, inputStreamReader.toString());
    do {
    read = inputStreamReader.read(buffer, 0, buffer.length);
    Log.d(TAG, "Read val" + read);
    if (read>0) {
    Log.d(TAG, "Readdinggg dataaa");
    readData.append(buffer, 0, read);
    }
    } while (read>=0);
    Log.d(TAG, "out of the while loop");
    Log.d(TAG, "Received data" + readData.toString());
    //httpStuff.setText(readData);

    } catch (IOException e) {
    Log.d(TAG, "IO exception");
    } catch (IllegalStateException e) {
    Log.d(TAG, "IllegalStateException");
    } catch (IndexOutOfBoundsException e ) {
    Log.d(TAG, "Index out of bound exception");
    }
    }
    }
    }
    }

    HttpRequestAsync.java:
    public class HttpRequestAsync extends AsyncTask<HttpUriRequest, Integer, HttpResponse> {

    private static String TAG = "Httpclient";
    private HttpClient mHttpClient;
    private HttpContext mHttpContext;

    /**
    * Constructor
    */
    HttpRequestAsync(Activity activity, HttpClient httpClient, HttpContext httpContext) {

    // Initialize the member variables
    mHttpClient = httpClient;
    mHttpContext = httpContext;

    // Create and initialize progress bar
    }

    /**
    * Sends the input Http request in background thread
    */
    @Override
    protected HttpResponse doInBackground(HttpUriRequest... request) {

    try {

    // Send the Http Request
    return mHttpClient.execute(request[0], mHttpContext);

    } catch (ClientProtocolException e) {

    // Send the error log to UI thread to update in the dialog
    Log.d(TAG, e.getMessage());


    } catch (IOException e) {

    // Send the error log to UI thread to update in the dialog
    Log.d(TAG, e.getMessage());
    }

    return null;
    }

    /**
    * Runs on the UI thread.
    * Starts a progress dialog and displays it.
    */
    protected void onProgressUpdate (Integer... values) {
    super.onProgressUpdate(values[0]);
    //mDownloadProgressBar.show();
    }

    /**
    * Gets the return value returned by doInBackground() and calls received response of HttpClient
    */
    protected void onPostExecute (HttpResponse response) {
    super.onPostExecute(response);

    //mDownloadProgressBar.hide();
    mHttpClient.receivedResponse(response);
    }

    }
    04-22-2013 05:44 AM

Similar Threads

  1. how to root/get root access in android emulator?
    By preetb123 in forum General News & Discussion
    Replies: 0
    Last Post: 02-23-2011, 12:20 PM
  2. how to root/get root access in android emulator?
    By preetb123 in forum Android Apps
    Replies: 0
    Last Post: 02-23-2011, 12:15 PM
  3. How to get the call log details in android?
    By aptgopal in forum Android Apps
    Replies: 0
    Last Post: 01-28-2011, 03:14 PM
  4. Replies: 0
    Last Post: 09-30-2010, 11:18 AM
LINK TO POST COPIED TO CLIPBOARD