Hello, I have this code that I want to make on android.
Here is the PHP code
<?php
mysql_connect("localhost","username","password");
mysql_select_db("game");
echo "Database has been selected";
$a=mysql_query("SELECT * from game1 where id>'".$_REQUEST['id']."'");
while($b=mysql_fetch_assoc($a))
$output[]=$b;
print(json_encode($output));
?>
Here is the android code
package org.me.androidapplication21;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author admin-heenashree
*/
public class two extends Activity {
String KEY_121 = "http://192.168.0.150/index.php";
InputStream is;
String result;
//the roll data to send
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id","2"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("KEY_121");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new
InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result=sb.toString();
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", Question: "+json_data.getString("Question")
);
}
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}
The application stops unexpectedly. IDK why is that?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en