很抱歉打扰您,因为我是 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;
?>
顺便说一句,我从一个在线网站获得了这些代码。但是,如果这些代码能够工作,我应该能够将它实施到我的项目中。