Android应用程序无法从MYSQL获取数据

时间:2022-09-25 16:00:31

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

我是android新手。我遇到的问题是当我运行我的应用程序时,它不显示从mysql在线服务器获取数据。虽然我的PHP脚本很好,因为当我在浏览器中打开我的PHP脚本链接时,它会显示这个JSON字符串

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

{ “ID” 为 “1”, “mobileName”: “诺基亚”, “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.

但是当我从我的Android应用程序调用此脚本时,该应用程序在我的真实设备或模拟器上运行,它在该行上显示错误。

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

logcat :

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

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 个解决方案

#1


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.

问题在于您的免费服务器意味着您的在线Web服务器将cookie发送到未处理的android。现在您可以使用另一个网站托管站点而不是base.pk,byethost.com。使用ueuo.com摆脱这些问题。

#2


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.

问题是你的服务器。您看到的JSON字符串呈现在客户端。真正的源代码与您发布的普通字符串不同。看一下源代码(对不起截图),以便更好地了解实际问题。

#1


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.

问题在于您的免费服务器意味着您的在线Web服务器将cookie发送到未处理的android。现在您可以使用另一个网站托管站点而不是base.pk,byethost.com。使用ueuo.com摆脱这些问题。

#2


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.

问题是你的服务器。您看到的JSON字符串呈现在客户端。真正的源代码与您发布的普通字符串不同。看一下源代码(对不起截图),以便更好地了解实际问题。