Android代码将http请求发送到本地服务器

时间:2015-08-23 14:48:55

标签: android http httpurlconnection iot

我已经实现了HttpUrlConnection来向本地http服务器(公共LAN,Wifi,WifiAP)发送请求。该代码适用于普通的Web服务器(www.xyz.com),但对本地服务器(192.168.x.y:z)无法正常工作。

代码:

1)发出请求的功能

public static String getDef(String urlInput) {
        URL url = null;
        try {
            url = new URL("http://" + urlInput);
            Log.d("http", "calling " + url.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn  = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Connection", "close");
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.connect();
            InputStream in = null;
            in = (conn.getInputStream());
            String response =readIt(in,5) ;
            //response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
            return response;
        } catch (IOException e) {
            Log.e("http","Error");
            e.printStackTrace();
        }
        return "LocalClient didnt work";
    }

2)调用上述功能的功能。

new AsyncTask<String, Void, String>(){
    @Override
    protected String doInBackground(String... params){
    try {
        return LocalClient.getDef(URL);
    } catch (Exception e){
        e.printStackTrace();


    }
    return "Didnt work";
}
@Override
protected void onPostExecute(String string) {
    message.append(string);
}
}.execute();

3)在服务器上收到带有以下标题的请求。

GET / HTTP/1.1
Connection: close
User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 5 Build/LYZ28E)
Host: 192.168.0.101
Accept-Encoding: gzip

如果有人能指出我正确的方向,那就太好了。

我也尝试过OkHttp,

public static String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = client.newCall(request).execute();
            resp= response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resp;
    }

使用普通的Web服务器时,响应被正确返回(源),但在使用本地服务器时,我收到以下错误

java.io.EOFException
         W/System.err﹕ at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:95)
         W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:179)
         W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
         W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:628)
         W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388)
         W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
         W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
         W/System.err﹕ at com.demo.demo.http.LocalClient.getDef(LocalClient.java:56)
         W/System.err﹕ at com.demo.demo.wifi.connector$2$2.doInBackground(connector.java:109)
         W/System.err﹕ at com.demo.demo.wifi.connector$2$2.doInBackground(connector.java:105)
         W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
         W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
         W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
         W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
         W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
         W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:1)

问题是服务器端,并通过使用适当的标头解决。 OkHttp似乎仍然没有工作和函数&#34; public static String getDef(String urlInput)&#34;上面提到的工作完美无瑕。

答案 1 :(得分:0)

似乎有一个关于这个问题的公开错误: https://github.com/square/okhttp/issues/1518

他们建议将android:vmSafeMode="true"添加到清单文件中。