相关疑难解决方法(0)

Android JSON HttpClient使用HttpResponse将数据发送到PHP服务器

我目前正在尝试将一些来自Android应用程序的数据发送到php服务器(两者都由我控制).

在应用程序中的表单上收集了大量数据,这些数据被写入数据库.这一切都有效.

在我的主要代码中,首先我创建了一个JSONObject(我在这里为此示例删除了它):

JSONObject j = new JSONObject();
j.put("engineer", "me");
j.put("date", "today");
j.put("fuel", "full");
j.put("car", "mine");
j.put("distance", "miles");
Run Code Online (Sandbox Code Playgroud)

接下来,我将对象传递给发送,并收到响应:

String url = "http://www.server.com/thisfile.php";
HttpResponse re = HTTPPoster.doPost(url, j);
String temp = EntityUtils.toString(re.getEntity());
if (temp.compareTo("SUCCESS")==0)
{
    Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)

HTTPPoster类:

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    HttpEntity entity;
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    entity = s;
    request.setEntity(entity);
    HttpResponse response;
    response = …
Run Code Online (Sandbox Code Playgroud)

php android json httpresponse httpclient

23
推荐指数
1
解决办法
6万
查看次数

标签 统计

android ×1

httpclient ×1

httpresponse ×1

json ×1

php ×1