HttpResponse返回null

时间:2014-07-03 07:11:50

标签: android null httpresponse

我想使用以下php脚本搜索一个diretory,并通过Android应用程序获取显示的JSONArray。但每次我调用Android应用程序时,HttpResponse为null,我不知道为什么。

PHP:

`<?php
$pfad = "./months";
$files = scandir($pfad);
$result = array();
for($a = 2; $a < count($files); $a++)
   {
   $result[] = $files[$a];
   }
print json_encode($result);
?>`

如果我在服务器上打开脚本,我会得到[&#34; 5.txt&#34;,&#34; June2014.txt&#34;]。

机器人:

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    JSONArray finalResult;
    Integer responseCode;
    TextView show;


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

        new MyAsyncTask().execute();

        show = (TextView)findViewById(R.id.textView1);
        show.setText(String.valueOf(responseCode));

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private class MyAsyncTask extends AsyncTask<String, Integer, Integer>{

        @Override
        protected Integer doInBackground(String... params) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("myurl.php");

            try {
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            int responseCode = response.getStatusLine().getStatusCode();

            } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            return null;

        }


        }

}

注意:myurl.php确实是正确的服务器地址。

我很乐意提供任何帮助

2 个答案:

答案 0 :(得分:0)

将两个代码行放在

之后
new MyAsyncTask().execute()

在异步任务的onPostExecute()中。你现在对他们来说太早了。

答案 1 :(得分:0)

您可以通过设置变量同步方式来运行,但线程是异步的,那么当TextView变量设置还没有数据时,为什么会给出NullPointerException。

变化:

public class MainActivity extends Activity {

JSONArray finalResult;
TextView show;


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

    new MyAsyncTask().execute();

    show = (TextView)findViewById(R.id.textView1);
    show.setText(String.valueOf(responseCode));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class MyAsyncTask extends AsyncTask<String, Integer, Integer>{

    @Override
    protected Integer doInBackground(String... params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("myurl.php");

        try {
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        int responseCode = response.getStatusLine().getStatusCode();

        } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return null;
    }
    }
}

为:

public class MainActivity extends Activity {

JSONArray finalResult;
Integer responseCode;
TextView show;


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

    show = (TextView)findViewById(R.id.textView1);
    new MyAsyncTask().execute();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class MyAsyncTask extends AsyncTask<String, Integer, Integer>{

    int responseCode;

    @Override
    protected Integer doInBackground(String... params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("myurl.php");

        try {
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        responseCode = response.getStatusLine().getStatusCode();

        } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
       // TODO Auto-generated method stub
       super.onPostExecute(result);
       show.setText(String.valueOf(responseCode));        
    }

    }

   }