Wednesday, 28 August 2013

JSON parsing in Android using PHP MySQL Null Pointer Exception

JSON parsing in Android using PHP MySQL Null Pointer Exception

Hi i am doing json parsing in android with php but i am getting null
pointer exception , i am using port 8080, please help me: ##
link :
http://fahmirahman.wordpress.com/2011/04/26/the-simplest-way-to-post-parameters-between-android-and-php/
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String result = null;
InputStream is = null;
StringBuilder sb=null;
ArrayList nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("sku","sku"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://192.168.5.61:8080/KeausMapPolicyDefender/android_json.php");
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);
sb = new StringBuilder();
sb.append(reader.readLine() );
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
int fd_id;
String fd_name;
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
fd_id=json_data.getInt("sku");
fd_name=json_data.getString("violation_amount");
}
}catch(JSONException e1){
Toast.makeText(getBaseContext(), "No Violation Found",
Toast.LENGTH_LONG).show();
}catch (ParseException e1){
e1.printStackTrace();
}
}
}

No comments:

Post a Comment