0

I have tried AsyncTask as others suggested when trying to send http request to PC server. The variable resultString was created and assigned a voice recognition result. The codes before this part worked just fine.

The problematic code is as belowed:

private class HttpGetTask extends AsyncTask<Void , Void, String> {



private String URL = "http://192.168.1.100:9080/" + "?" +"content1="+resultString;
//buf.append("content1="+URLEncoder.encode(resultString,"UTF-8"));
AndroidHttpClient mClient = AndroidHttpClient.newInstance("");
//Log.e("check", "succeed");
@Override
protected String doInBackground(Void... param) {

    HttpGet request = new HttpGet(URL);
    //Log.e("check1","succeed");
    Log.e("url",URL);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    try {
        Log.e("check3","succeed");
        return mClient.execute(request, responseHandler);


    } catch (ClientProtocolException exception) {
        exception.printStackTrace();
        Log.e("check4","succeed");


    } catch (IOException exception) {
        exception.printStackTrace();
        Log.e("check5","succeed");


    }
    Log.e("check6","succeed");//to this point
    return null;
}

@Override
protected void onPostExecute(String result) {

    if (null != mClient)
        mClient.close();
    //Log.e("check6","succeed");
    mTextView.setText(result);

}
} 

the execute part looks like this at the end of the whole codes:

new HttpGetTask().execute();

Logcat

 03-06 14:57:07.117: W/System.err(12451): org.apache.http.conn.ConnectTimeoutException:
 Connect to /192.168.1.100:9080 timed out 03-06 14:57:07.137: W/System.err(12451): 
 at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
 03-06 14:57:07.137: W/System.err(12451): at 
 org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)

the Log shows that there is an IOException in my codes and the value of mClient will eventually be null.I have looked online for the same situation and it seems this occurs when data takes too much time to respond that is requested. And many of the answers suggested I should reset Timeout and allow more time for the request to be sent.(Maybe the network is unreliable..) But I don't really understand where to put the reset codes in my situation. Can anyone help with this? Thanks.

1 Answer 1

0

You need to check if your URL :-

http://192.168.1.100:9080/

Is Working fine or not. The exception is ConnectTimeoutException which points to the fact that the URL is inaccessible at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.