Android中的HttpPost问题

时间:2018-09-16 10:33:44

标签: android http-post

这是我的android项目中的MainActivity.java文件。

我想接收页面的HTML源代码。

运行代码时,结果为Exception : null

问题出在哪里?

public class MainActivity extends AppCompatActivity {

    private HttpPost httppost;
    private HttpResponse response;
    private HttpClient httpclient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textv = (TextView) findViewById(R.id.textv);

        try{

            httpclient=new DefaultHttpClient();

            httppost= new HttpPost("http://example.com/");

            response=httpclient.execute(httppost);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);

            textv.setText(response);

        }catch(Exception e){
            textv.setText("Exception : " + e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您确实使用http://example.com/,那是因为response为null。

更新1

尝试此代码:

public static String readUrl(String url, ArrayList<NameValuePair> params, Context context) {
        HttpClient client = null;
        HttpPost method = null;
        InputStream inputStream = null;
        String result = null;
        HttpResponse response = null;
        try {
            final HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,
                    context.getResources().getInteger(R.integer.serverConnectionTimeout));
            HttpConnectionParams.setSoTimeout(httpParams,
                    context.getResources().getInteger(R.integer.serverConnectionTimeout));

            client = new DefaultHttpClient(httpParams);
            method = new HttpPost(url);

            if (params != null) {
                method.setEntity(new UrlEncodedFormEntity(params));
            }
            response = client.execute(method);
            inputStream = response.getEntity().getContent();
            result = convertInputStreamToString(inputStream);
        } catch (ClientProtocolException ex) {
            Log.e("ClientProtocolException", "" + ex.getMessage());
            ex.printStackTrace();

        } catch (IOException ex) {
            Log.e("IOException", "" + ex.getMessage());
        } finally {
            try {
                if (client != null) {
                    client.getConnectionManager().closeExpiredConnections();
                }
            } catch (Exception e) {
                Log.e("IOException2", "" + e.getMessage());
            }
        }
        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder builder = new StringBuilder();

            String line;

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            return builder.toString();
        } catch (IOException ex) {
            Log.e("InputStreamToString", "" + ex.getMessage());
        }

        return null;
    }