2014-02-19 315 views
0

我是android开发中的新成员。我使用PHP将我的android应用程序连接到mysql,并使用“Json”来编码。我使用以下代码获取数据,将响应转换为字符串 解析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 

    } 

} 

好心帮它。

+0

你有没有创建** main布局文件夹中的mainhome.xml **? – Piyush

+0

Yes.i created.a简单的布局,命名为“mainhome”。只适用于显示来自mysql的数据 –

+0

你在哪里显示数据在mainhome中?解析json数据后没有发现任何代码.. – Brendon

回答

0

试试这个..

只是删除import android.R.string;这一行

0

你正在做一个MainActivity的网络尝试,这将引发一个异常并强制关闭您的项目,所以它使用通过doInBackground方法的AsyncTask ..

了解AsyncTask

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