无线上的空HTTP响应,但在LAN上工作

时间:2015-05-16 00:19:36

标签: android httpresponse

我创建了一个Android应用程序,用于从网页中读取数据并从HTTP响应中提取数据。我定义了一个异步任务。只要我连接到XAMMP或我的局域网上的实际目标服务器,该应用程序完美无缺,当我在我的局域网上但通过互联网使用WiFi(我为此设置端口转发)连接时,它工作得很好,但是当我在手机上关闭WiFi,以便将互联网连接到目标URL,我从HTTP GET请求得到一个空响应。

由于它适用于快速网络,我认为问题不在于响应的长度 - 但我不是专家,所以不确定......

这是HTTP代码:

nil

这是异步任务:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get reference to the views
    etCurrentPower = (EditText) findViewById(R.id.fieldCurrentPower);
    public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
        result = "Exception in http request";
    }

    return result;

    private static String convertInputStreamToString(InputStream        inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

我从UI按钮调用Async任务,如下所示:

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        return GET(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show(); // then I do search for a matching pattern and display that in ViewText field in the app.

知道如何解决这个空响应吗?我尝试将结果发布到日志中,但我得到的只是一个空字符串,它确实告诉我我得到了“响应”,但不是为什么它是空的......

干杯, 加埃塔诺。

0 个答案:

没有答案