Hi, i am new to this forum. I have also just started on android programming. I seem to be stuck a point where i cannot seem to solve.
This happened when i was trying to call data from an online database using Json. I was hoping anyone could enlighten me on this issue. Thanks, the source code is below.
The error message log is written below.
09-30 06:54:13.285: ERROR/log_tag(223): Error converting result java.lang.NullPointerException
09-30 06:54:13.295: ERROR/log_tag(223): Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 0 of
This happened when i was trying to call data from an online database using Json. I was hoping anyone could enlighten me on this issue. Thanks, the source code is below.
Code:
package com.mysql;
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;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class mysql extends Activity {
private static final InputStream is = null;
/** Called when the activity is first created.
* @param is */
@SuppressWarnings("null")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("location","Tampines"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.niccloud.byethost12.com/GetMall.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream 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");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
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")+
", mall_name"+json_data.getString("mall_name")+
", location"+json_data.getString("location")+
", telephone"+json_data.getString("telephone")+
", area"+json_data.getString("area")+
", email"+json_data.getString("email")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
09-30 06:54:13.285: ERROR/log_tag(223): Error converting result java.lang.NullPointerException
09-30 06:54:13.295: ERROR/log_tag(223): Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 0 of