Android的HTTP响应处理

Android的HTTP响应处理

本文介绍了Android的HTTP响应处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code发送一个HTTP请求:

here's my code that sends an HTTP Request :

package com.example.database;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

public class post extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    postData();
}


public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost("http://192.168.83.1/test.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();

        String responseBody = httpclient.execute(httppost, responseHandler);
        Log.d("test", responseBody);
       // Toast.makeText(getApplicationContext(), responseBody, Toast.LENGTH_LONG).show();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

该文件的内容的 test.php的是:

 <?php

echo"works";

?>

在Log.d(测试,responseBody);标签不出现到LogCat.However,如果我转向它几行(responseBody前),并把静态一段文​​字,它的工作

the Log.d("test", responseBody); tag doesn't appear onto LogCat.However , if I shift it up a few lines (before responseBody) and put a static piece of text , it does work

是否存在与HTT prequest有问题吗?它怎么可能得到解决。

Is there a problem with the HTTPRequest ? How could it be solved

谢谢!

推荐答案

删除BasicResponseHandler后再试。它应该工作。在Menifest检查perticuler许可

try after removing BasicResponseHandler. it should work. and check in Menifest for perticuler permission

HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());

这篇关于Android的HTTP响应处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:58