通过php web服务连接android与sql

时间:2015-10-20 04:05:26

标签: php android mysql httpclient

我已将一些数据存储在网上存储的mysql数据库中。我在web上使用php脚本,在应用程序中使用JSON Object但是在解析数据时遇到错误。

我能够看到来自MySql服务器的网页中的数据。但是当运行应用程序并尝试获取数据时,Process Dialogue不会结束,并且会从catch块中抛出以下解析错误。

错误记录

Error parsing data org.json.JSONException: Value <html><body><script of type java.lang.String cannot be converted to JSONArray

这是我的代码。 AddTopic.java

public class AddTopic extends Activity implements View.OnClickListener {
Button fetch;
TextView text;
EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);

    fetch= (Button) findViewById(R.id.fetch);
    text = (TextView) findViewById(R.id.text);
    et = (EditText) findViewById(R.id.et);
    fetch.setOnClickListener(this);
}

class task extends AsyncTask<String, String, Void>
{
    private ProgressDialog progressDialog = new ProgressDialog(AddTopicFragment.this);
    InputStream is = null ;
    String result = "";
    protected void onPreExecute() {
        progressDialog.setMessage("Fetching data...");
        progressDialog.show();
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                task.this.cancel(true);
            }
        });
    }
    @Override
    protected Void doInBackground(String... params) {
        String url_select = "http://swachapp.byethost32.com/demo.php";

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            //read content
            is =  httpEntity.getContent();


        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection "+e.toString());
            //Toast.makeText(MainActivity.this, "Please Try Again", Toast.LENGTH_LONG).show();
        }
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8),8);
            StringBuilder sb = new StringBuilder();
            String line = "";
            while((line=br.readLine())!=null)
            {
                sb.append(line+"\n");
            }
            is.close();
            result=sb.toString();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        return null;

    }
    protected void onPostExecute(Void v) {

        // ambil data dari Json database
        try {
            JSONArray Jarray = new JSONArray(result);
            for(int i=0;i<Jarray.length();i++)
            {
                JSONObject Jasonobject = null;
                //text_1 = (TextView)findViewById(R.id.txt1);
                Jasonobject = Jarray.getJSONObject(i);

                //get an output on the screen
                //String id = Jasonobject.getString("id");
                String name = Jasonobject.getString("headings");
                String db_detail="";

                if(et.getText().toString().equalsIgnoreCase(name)) {
                    db_detail = Jasonobject.getString("detailstory");
                    text.setText(db_detail);
                    break;
                }
                //text_1.append(id+"\t\t"+name+"\t\t"+password+"\t\t"+"\n");

            }
            this.progressDialog.dismiss();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()) {
        case R.id.fetch : new task().execute();break;
    }

}
}

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

该问题与Android或PHP无关。它与您的托管服务器有关。它期望cookie和javascript处理请求。当我浏览Google Chrome中的网址时,它会将结果显示为JSONArray。当我尝试使用Android HttpClient运行url时,它会生成结果为       &#34; ....&#34;

检查以下链接中的答案。 Getting HTML response instead of JSON in android