关于android发送get请求

时间:2014-03-05 11:41:32

标签: android http request

这里我在Android中获取请求时遇到问题。 我已经在我的PC上使用webapp2运行服务器程序,我成功地将请求从自身发送到服务器程序。 但是,当我尝试从手机发送请求时,程序似乎总是跳过捕获(异常e)部分。三个Log.e()工作正常,因为我可以在日志记录中看到所有这些。谁能告诉我这里可能出现什么问题?是我的电脑用来阻止请求的防火墙吗?提前致谢。 PS:变量resultString是我从语音识别中得到的结果。

try{
            StringBuilder buf = new StringBuilder("http://192.168.1.100:9080/");
            buf.append("?");
            buf.append("content1="+URLEncoder.encode(resultString,"UTF-8"));
            //buf.append(resultString);

            //Toast.makeText(MainActivity.this, buf, Toast.LENGTH_SHORT).show();

            URL url = new URL(buf.toString());
            Log.e("check1","url_transformation works ok");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            Log.e("check2","open connection works ok");
            conn.setRequestMethod("GET");
            Log.e("check3","set get works ok");

            if(conn.getResponseCode()==200){
                Log.e("check4","get works");
                Toast.makeText(MainActivity.this, "GET works", Toast.LENGTH_SHORT).show();
            }
            else 
                {
                Log.e("check5","get fails");
                Toast.makeText(MainActivity.this, "GET fails", Toast.LENGTH_SHORT).show();
                }
            }
            catch(Exception e){
                Toast.makeText(MainActivity.this, "other problems occur", Toast.LENGTH_SHORT).show();
            }

03-05 20:47:19.486:W / System.err(16066):android.os.NetworkOnMainThreadException 03-05 20:47:19.486:W / System.err(16066):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) 03-05 20:47:19.486:W / System.err(16066):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 03-05 20:47:19.486:W / System.err(16066):at libcore.io.IoBridge.connectErrno(IoBridge.java:127) 03-05 20:47:19.486:W / System.err(16066):at libcore.io.IoBridge.connect(IoBridge.java:112) 03-05 20:47:19.486:W / System.err(16066):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 03-05 20:47:19.486:W / System.err(16066):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 03-05 20:47:19.486:W / System.err(16066):at java.net.Socket.connect(Socket.java:872) 03-05 20:47:19.486:W / System.err(16066):at libcore.net.http.HttpConnection。(HttpConnection.java:77)

我认为异常日志如上所述。那么这里有什么问题?

我已经尝试过AsyncTask,就像你们有人建议的那样。 代码如下:

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);

    }
} 

03-06 14:57:07.117:W / System.err(12451):org.apache.http.conn.ConnectTimeoutException:连接到/192.168.1.100:9080超时 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)

日志显示我的代码中存在 IOException ,mClient的值最终将为null。我已经在线查看相同的情况,而且当数据占用太多时间时似乎会发生这种情况回应所要求的。许多答案表明我应该重置Timeout并允许更多时间发送请求。但我真的不明白在我的情况下把重置代码放在哪里。有人能帮忙吗?感谢。

2 个答案:

答案 0 :(得分:1)

当您尝试在主/ UI线程上执行网络操作时,会抛出NetworkOnMainThreadException。这在Android中是不允许的。相反,您需要在后台线程中执行网络操作。您可以使用AsyncTask

执行此操作

答案 1 :(得分:1)

您可以将其放在onCreate()上,但最好的选择是使用AsyncTask

StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(threadPolicy);