Android Json HTTP Post, Unable to parse data
<p>Sorry to trouble you, as I am new to Android programming, and i have face the following problem while trying retrieve my send and receive a response from my local host server. </p>

<p>The program seem to auto close when i try to launch it at first. But after implementing </p>

<pre><code>StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
</code></pre>

<p>The program is able to run, however, the data is not parse across. </p>

<p>I have allowed the Internet permission in my android Manifest script.</p>

<p>My Android Codes</p>

<pre><code>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
}
}
}
</code></pre>

<p>And lastly, my PHP codes</p>

<pre><code>&lt;?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-&gt;name;
$pos = $data-&gt;position;
echo "Result: \n";
echo "--------------\n";
echo "Name     : ".$name."\n Position : ".$pos;
?&gt;
</code></pre>

<p>By the way, I got these codes from an online website. However, if these codes are able to work, i should be able to implement it into my project. </p>
