2014-02-19 74 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,但從未使用過之後的數據..