0

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.

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

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

The program is able to run, however, the data is not parse across.

I have allowed the Internet permission in my android Manifest script.

My Android Codes

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
}
}
}

And lastly, my PHP codes

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

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.

1 Answer 1

1

You are trying to access the network on the main thread. This is a very bad idea. You need to do the network access in a separate thread instead. The linked article provides several guidelines for doing this.

If you still have problems after moving the network access to a different thread, then feel free to come back and ask more questions.

(The reason your app was killed is because the Android system noticed it hadn't responded for a while, since it was waiting for network traffic.)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks for the feedback.Actually i tried running it on a different thread but it was still not working. I had found out the error causing it was that android doesnt accept localhost but needs an IP to send the the variables to.Thanks =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.