2015-02-09 98 views
0

我想在我的Android應用程序中建立一個連接到MYSQL數據庫。Android片段JSON <!DOCTYPE類型java.lang.String不能轉換爲JSONArray

我已經下列本教程:http://www.trustingeeks.com/connect-android-app-to-mysql-database/

其基本上填充從JSON請求一個TextView。

區別在於我需要獲取數據連接在片段內工作。

這是我遇到問題的地方,什麼都沒有發生。下面是我嘗試實現教程成片段:

public class FragmentFour extends Fragment{ 

private String jsonResult; 
private String url = "http://cpriyankara.coolpage.biz/employee_details.php"; 
private TextView resultView; 
private static View view; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    if (view != null) { 
     ViewGroup parent = (ViewGroup) view.getParent(); 
     if (parent != null) 
      parent.removeView(view); 
    } 
    try { 
     view = inflater.inflate(R.layout.fragment_four, container, false); 


     resultView = (TextView) view.findViewById(R.id.result); 
     StrictMode.enableDefaults(); 
     getData(); 


    } catch (InflateException e) { 

    } 
    return view; 
} 


public void getData(){ 

    String result = ""; 

    InputStream isr = null; 

    try{ 

     HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost("http://ieeehiit.host22.com/myfile.php"); //YOUR PHP SCRIPT ADDRESS 

     // HttpPost httppost = new HttpPost("http://172.23.193.32/elift-test/myfile.php"); //YOUR PHP SCRIPT ADDRESS 

     HttpResponse response = httpclient.execute(httppost); 

     HttpEntity entity = response.getEntity(); 

     isr = entity.getContent(); 

    } 

    catch(Exception e){ 

     Log.e("log_tag", "Error in http connection "+e.toString()); 

     resultView.setText("Couldnt connect to database"); 

    }///convert response to string 

    try{ 

     BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8); 

     StringBuilder sb = new StringBuilder(); 

     String line = null; 

     while ((line = reader.readLine()) != null) { 

      sb.append(line + "\n"); 

     } 

     isr.close(); 

     result=sb.toString(); 

    } 

    catch(Exception e){ 

     Log.e("log_tag", "Error converting result " + e.toString()); 

    }//parse json data 

    try { 

     String s = ""; 

     JSONArray jArray = new JSONArray(result); 

     for(int i=0; i<jArray.length();i++){ 

      JSONObject json = jArray.getJSONObject(i); 

      s = s +"Name : "+json.getString("id")+" "+json.getString("username"); } 

     resultView.setText(s); 

    } catch (Exception e) 

{// TODO:處理異常

 Log.e("log_tag", "Error Parsing Data "+e.toString()); 

    } 

} 

}

一切執行罰款,但沒有什麼是TextView的發生。我看着在logcat中,發現:

"2-09 21:06:05.155 13519-13519/com.example.nottinghamtouristapp E/log_tag﹕ Error Parsing Data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray" 

我是很新的Android開發,所以這個問題可能是由於我缺乏有經驗的fragments.I不道歉,如果我似乎愚蠢的傲慢和張貼張貼求助。我覺得所有想到的都耗盡了!

非常感謝您提前。

+3

錯誤說明了一切:服務器的「json」響應是** NOT ** json。它是HTML。一個json響應不能以'<!DOCTYPE'開始...... – 2015-02-09 21:37:27

回答

1

好的嘗試這種方法

Iniatialize變量

private View rootView; 
private ListView lv; 
private String jsonResult; 
private String url = "http://cpriyankara.coolpage.biz/employee_details.php"; 
ProgressDialog pDialog; 
private TextView resultView; 

修改您的AsyncTask

public class JsonReadTask extends AsyncTask<String , Void, String> { 
     public JsonReadTask() { 
      super(); 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_DEVICE_DEFAULT_DARK); 
      pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      pDialog.setIndeterminate(true); 
      pDialog.setMessage("Your message"); 
      pDialog.setCancelable(false); 
      pDialog.setInverseBackgroundForced(true); 
      pDialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(params[0]); 
      try { 
       HttpResponse response = httpclient.execute(httppost); 
       jsonResult = inputStreamToString(
         response.getEntity().getContent()).toString(); 
       customList = new ArrayList<>(); 

       JSONObject jsonResponse = new JSONObject(jsonResult); 
       JSONArray jsonMainNode = jsonResponse.optJSONArray("array-name"); 
       for (int i = 0; i < jsonMainNode.length(); i++) { 
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
        String name = jsonChildNode.optString("name"); 

       } 
       return name; 
      } catch (Exception e) { 
       e.printStackTrace(); 
       getActivity().finish(); 
      } 
      return null; 
     } 

     private StringBuilder inputStreamToString(InputStream is) { 
      String rLine = ""; 
      StringBuilder answer = new StringBuilder(); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
      try { 
       while ((rLine = rd.readLine()) != null) { 
        answer.append(rLine); 
       } 
      } catch (Exception e) { 
       getActivity().finish(); 
      } 
      return answer; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      if(customList == null){ 
       Log.d("ERORR", "No result to show."); 
       return; 
      } 
      resultView.setText(result); 
      pDialog.dismiss(); 
     } 
    }// end async task 

    public void accessWebService() { 
     JsonReadTask task = new JsonReadTask(); 
     task.execute(new String[]{url}); 
    } 

希望它可以幫助!

+1

謝謝你幫了很多! :) – user3882091 2015-02-12 17:51:01

+0

很高興我幫助! – 2015-02-12 19:21:20

相關問題