Here is my class where I am trying to get some data from MYSQL using JSON in JSONArray.
This gives me an error.
public class ConnectMySql extends Activity{
InputStream is;
JSONArray jArray;
StringBuilder sb;
TextView textv;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textv = (TextView) findViewById(R.id.tvHI);
setContentView(R.layout.main);
try{
HttpClient httpsClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://79.114.48.119/data.php");
List<NameValuePair> nameValue = new ArrayList<NameValuePair>();
httpPost.setEntity(new UrlEncodedFormEntity(nameValue));
HttpResponse httpResponse = httpsClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in HTTP connection" + e.toString());
Toast.makeText(this, "http connection error: " + e.toString(), Toast.LENGTH_LONG).show();
}
try{
BufferedReader BufR = new BufferedReader (new InputStreamReader(is, "iso-8859-1"),8);
sb = new StringBuilder();
sb.append(BufR.readLine() + "\n");
String line = "0";
while((line = BufR.readLine()) != null){
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error in convert String" + e.toString());
}
int lat;
int lon;
try{
jArray = new JSONArray(result);
Toast.makeText(this, "no data found" + result, Toast.LENGTH_LONG).show();
JSONObject json_data = null;
for(int i = 0; i< jArray.length(); i++){
json_data = jArray.getJSONObject(i);
lat = json_data.getInt("latitude");
lon = json_data.getInt("longitude");
textv.setText(lat + lon);
}
}catch(JSONException e){
Toast.makeText(this, "no data found", Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(this, "error" + e.toString(), Toast.LENGTH_LONG).show();
}
}
}
It does not change the textual representation, and this gives me a NullPointException.
What should I do? Can someone help me or give me some advice? Thank!
source
share