数据库连接错误

时间:2014-02-19 09:11:40

标签: android json

我是android开发的新手。我使用PHP将我的Android应用程序连接到mysql,并使用" Json"到encoding.i使用以下代码来获取数据,将响应转换为字符串 解析JSON数据。 但是我对这段代码有错误" JSONArray jArray = null;" 错误是" mainhome(布局)无法解析,或者不是字段。 java代码如下。

package com.example.bagyourjob;

import android.net.ParseException;
import android.os.Bundle;

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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.string;
import android.app.Activity;
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.EditText;

import android.widget.Toast;
import android.os.Bundle;

public class Test extends Activity implements OnClickListener {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainhome);

        JSONArray jArray = null;
        String result = null;
        StringBuilder sb = null;
        InputStream is = null;


        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //http post
        try{
             HttpClient httpclient = new DefaultHttpClient();

             //Why to use 10.0.2.2
             HttpPost httppost = new HttpPost("http://10.0.2.2/myfirsttry.php");
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity entity = response.getEntity();
             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);
               sb = new StringBuilder();
               sb.append(reader.readLine() + "\n");

               String line="0";
               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());
                }

        String name;
        try{
              jArray = new JSONArray(result);
              JSONObject json_data=null;
              for(int i=0;i<jArray.length();i++){
                     json_data = jArray.getJSONObject(i);
                     String ct_name = json_data.getString("NAME");//here "Name" is the column name in database
                 }
              }
              catch(JSONException e1){
               Toast.makeText(getBaseContext(), "No Data Found" ,Toast.LENGTH_LONG).show();
              } catch (ParseException e1) {
           e1.printStackTrace();
         }
        }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

请帮助它。

2 个答案:

答案 0 :(得分:0)

试试这个..

删除 import android.R.string;此行

答案 1 :(得分:0)

您正在MainActivity中进行网络尝试,这会引发异常并强制关闭您的项目,所以使用doInBackground方法使用AsyncTask进行。

了解AsyncTask

到目前为止,我已经看到您的代码似乎不完整,使用解析了JSON但之后从未使用过该数据..