Android Json HTTP Post,无法解析数据

时间:2012-04-10 15:24:32

标签: java android

很抱歉给您带来麻烦,因为我是Android编程新手,在尝试检索我的发送并从本地主机服务器接收响应时遇到以下问题。

当我尝试首先启动它时程序似乎自动关闭。但是在实施之后

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

程序能够运行,但数据不能解析。

我在Android Manifest脚本中允许了Internet权限。

我的Android代码

package kx.practice;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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 org.json.JSONException;
import org.json.JSONObject;

import android.widget.TextView;



public class JsonHttpPractice2Activity extends Activity {

TextView tv;
String text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

tv  = (TextView)findViewById(R.id.textView1);
text    = "";

try {
postData();
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("Error in JSON Exception 1");
e.printStackTrace();
}
}

public void postData() throws JSONException{ 
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/phpWebservice/AndroidTest.php");
JSONObject json = new JSONObject();

try {
// JSON data:
json.put("name", "Fahmi Rahman");
json.put("position", "sysdev");

JSONArray postjson=new JSONArray();
postjson.put(json);

// Post the data:
httppost.setHeader("json",json.toString());
httppost.getParams().setParameter("jsonpost",postjson);

// Execute HTTP Post Request
System.out.print(json);
HttpResponse response = httpclient.execute(httppost);

// for JSON:
if(response != null)
{
InputStream is = response.getEntity().getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
text = sb.toString();
System.out.println("This is my text" +text);
}

tv.setText(text);

}catch (ClientProtocolException e) {
System.out.println("Error in JSON Exception 2");
// TODO Auto-generated catch block
} catch (IOException e) {
System.out.println("Error in JSON Exception 3");
// TODO Auto-generated catch block
}
}
}

最后,我的PHP代码

<?php
$json = $_SERVER['HTTP_JSON'];
echo "JSON: \n";
echo "--------------\n";
var_dump($json);
echo "\n\n";

$data = json_decode($json);
echo "Array: \n";
echo "--------------\n";
var_dump($data);
echo "\n\n";

$name = $data->name;
$pos = $data->position;
echo "Result: \n";
echo "--------------\n";
echo "Name     : ".$name."\n Position : ".$pos;
?>

顺便说一句,我从在线网站上获得了这些代码。但是,如果这些代码能够工作,我应该能够将它实现到我的项目中。

1 个答案:

答案 0 :(得分:1)

您正尝试在主线程上访问网络。这是very bad idea。您需要在单独的线程中进行网络访问。链接的文章为此提供了一些指导。

如果在将网络访问权移至其他主题后仍有问题,请随时回来再提问。

(你的应用被杀的原因是因为Android系统注意到它没有响应一段时间,因为它正在等待网络流量。)