1

I am new in android. The problem which i faced is when i running my application it doesnot show data fetch from mysql online server. Although my php script is fine because when i open my php script link in browser it shows this JSON string

{"ID":"1","mobileName":"Nokia","mobileModel":"NA-85","mobilePrice":"8500"}.

But when i call this script from my android app which is either run on my real device or emulator it shows an error on that line.

int mobid = json.getInt("ID");

logcat :

03-23 22:38:29.151    1918-2302/com.example.abdul.sql E/Buffer Error﹕ Error  converting result org.json.JSONException: Value <html><body><script of type   java.lang.String cannot be converted to JSONObject
03-23 22:38:29.242    1918-1918/com.example.abdul.sql D/AndroidRuntime﹕  Shutting down VM
--------- beginning of crash
03-23 22:38:29.242    1918-1918/com.example.abdul.sql E/AndroidRuntime﹕ FATAL  EXCEPTION: main
Process: com.example.abdul.sql, PID: 1918
java.lang.NullPointerException: Attempt to invoke virtual method 'int  org.json.JSONObject.getInt(java.lang.String)' on a null object reference
        at  com.example.abdul.sql.MainActivity$Connectingdb.onPostExecute(MainActivity.java:172)
        at  com.example.abdul.sql.MainActivity$Connectingdb.onPostExecute(MainActivity.java:80)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

MainActivity.java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView tv;
String textview = null;
static JSONObject jObj ;
static String json = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
    Button b2 = (Button) findViewById(R.id.button1);

    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

// TODO Auto-generated method stub

            Intent i2 = new Intent(MainActivity.this,menuList.class);
            startActivity(i2);

        }

    });
    Button connectdb = (Button) findViewById(R.id.button1);
    connectdb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
// TODO Auto-generated method stub
            new Connectingdb().execute();
        }

    });
 }

 class Connectingdb extends AsyncTask<String, String, JSONObject> {
    /**

     * Before starting background thread Show Progress Dialog

     * */

    boolean failure = false;
    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Attempting connect...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }
    @Override
    protected JSONObject doInBackground(String... args) {
        InputStream is = null;

// Making HTTP request

        try {
// check for request method

            {

// request method is POST

// defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new   HttpPost("http://codedefault.base.pk/products.php");
                HttpResponse httpResponse =              httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }
        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

        } catch (ClientProtocolException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        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");

            }

            json = sb.toString();
            jObj = new JSONObject(json);
            is.close();
            return jObj;
        } catch (Exception e) {

            Log.e("Buffer Error", "Error converting result " + e.toString());
            return null;

        }
    }

    /**

     * After completing background task Dismiss the progress dialog

     * **/

    protected void onPostExecute(JSONObject json) {

        try {
            int mobid = json.getInt("ID");
            String mobilename = json.getString("mobileName");
            String mobileModel = json.getString("mobileModel");
            int mobilePrice = json.getInt("mobileprice");

            display("mobile Id= "+mobid+"\nmobileName = "+mobilename +"\n mobile model = "+mobileModel +"\n mobile price = "+mobilePrice+"\n>>>>>>>>>>>>>>>\n");


        } catch (JSONException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        }
        pDialog.dismiss();
    }
}
public void display(String m){
    tv.setText(m);
}
}

2 Answers2

0

The problem is your server. The JSON string you see it's rendered client-side. The real source code is different from the plain string you posted. Take a look at the source code (sorry for the screenshot) to better understand the actual problem.

Denys Vitali
  • 530
  • 6
  • 19
0

The problem is in your free server means that your online web server it sends the cookies to an android which is not handled. Now you can use another web hosting site than base.pk,byethost.com. Use ueuo.com to get rid of these problem.

user000758
  • 120
  • 12