Android, which attempts to parse JSON data for Android… here is a solution to the problem.
Android, which attempts to parse JSON data for Android
I’m trying to read my database from a remote database because I’m using php code to connect to my Mysql database and query my database:
File name: check.php:
<?php
require_once("php/dbconnect.php");
$query = "SELECT name FROM user ";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
while ( $row = mysql_fetch_array( $result ) ) {
$output[]=$row['name'];
}
print(json_encode($output));
mysql_close();
?/>
and Java (Android) code to connect to the remote database:
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;
import android.widget.Toast;
import java.net.URI;
public class ConnectToMyDb extends Activity {
InputStream is;
private HttpPost httppost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
http post
try{
HttpClient httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.Myurl.com/check.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
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");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
parse json data
try{
ArrayList<Messages> result1 = new ArrayList<Messages>();
JSONArray jArray = new JSONArray(result);
JSONObject json_data= new JSONObject(result);
for(int i=0; i<jArray.length(); i++){
json_data = jArray.getJSONObject(i);
Log.i("log_tag"," name: "+json_data.getString("name");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
}
My problem is not the concatenation, but the parsing of json and I get an exception :
Error parsing data org.json.JSONException:
Value <! DOCTYPE of type java.lang.String cannot be converted to JSONArray
Solution
I’ll remove this line :
JSONArray jArray = new JSONArray(result);
Instead, do this:
JSONObject json_data = new JSONObject(result);
JSONArray nameArray = json_data.names();
JSONArray valArray = json.toJSONArray(nameArray);
I believe this is correct, nevertheless, follow this tutorial, it will work for you :