in this simple code i'm sending username and password to server. if those are correct i must be get this result :
{"code":"1","credit":100000,"number":["500000072207"]}
but i get:
10-02 14:16:52.280 2299-2318/com.ms.app.ms E/Content﹕ org.apache.http.conn.EofSensorInputStream@4164ca78
for this sample my username and password is correct.
whats my problem in code? i think after using HttpPost
i must be get result another method such as HttpGet
. is that right?
My Code:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendJson("name","pass");
}
protected void sendJson(final String username, final String password) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost("http://www.example.com/test.php");
json.put("username", username);
json.put("password", password);
StringEntity se = new StringEntity( json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
if(response!=null){
//Get the data in the entity
InputStream in = response.getEntity().getContent();
Log.e("Content", String.valueOf(in));
}
} catch(Exception e) {
e.printStackTrace();
Log.e("Error", String.valueOf(e));
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
}
Thanks.