0

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

当我一开始尝试启动该程序时,它似乎会自动关闭。但实施后

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

该程序能够运行,但是,无法解析数据。

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

我的安卓代码

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;
?>

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

4

1 回答 1

1

您正在尝试访问主线程上的网络。这是一个非常糟糕的主意。您需要在单独的线程中进行网络访问。链接的文章为此提供了一些指导方针。

如果您在将网络访问权限移至其他线程后仍有问题,请随时回来提出更多问题。

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

于 2012-04-10T15:30:12.077 回答