• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps First Time Androider. App - JSON Webservice - MySQL

BearDy89

Lurker
Hi All,

Sorry if ive posted in the wrong place!

I am currently trying my hand at App Development.

I have a MySQL DB,
And am using PHP to output a JSON file,
I then want my App to read the JSON file, and output the results.

I Can view the JSON file in a web browser and it looks well formed,
but when i run my APP in the Eclipse Emulator, it simple outputs the package name and nothing else.


Heres what i have;


PHP - JSON
Code:
<?php
   
      $link = mysql_connect("sam-b.com.mysql","sam_b_com","*****");
         mysql_select_db("sam_b_com");
   

      $q=mysql_query("SELECT * FROM PIMSUsers");

        $output[]=$e;

         while($e=mysql_fetch_assoc($q))
   
              $output[]=$e;
   
      print(json_encode($output));
     
     print $output;
mysql_close();

     
?>


Android - Java
Code:
package testConnection.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 testMySql extends Activity {
    private static final InputStream is = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
 public static void main(String[] args) {


       
          String result = "";
       
          //the data to send
       
          ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
       
         
       
           
       
          //http post
       
          try{
       
                  HttpClient httpclient = new DefaultHttpClient();
       
                  HttpPost httppost = new HttpPost("http://sam-b.com/PIMS/getPeople.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("User_ID")+
      
                                  ", first name: "+json_data.getString("User_First_Name")+
      
                                  ", last name: "+json_data.getInt("User_Last_Name")
      
                                 
      
                          );
      
                  }
      
          }
      
          catch(JSONException e){
      
                  Log.e("log_tag", "Error parsing data "+e.toString());
      
          }

}
}



Many Thanks In Advance
Sam
 
Your main problem with this code is that the variable 'is' is never being assigned. You make a mistake on the line where you do:
InputStream is = entity.getContent();

First, that is scoped within the try, so it is not available outside that try. So when you use it later on, you are using the one in the class that you set to private static final for some reason and you assign it to null. That variable should not be final and the line above that I quoted should be:

is = entity.getContent();

-frank
 
yeah , I had the same problem [got the same code from the same tutorial site :P]
then I noticed that is is only inside the try and it got solved
 
Back
Top Bottom