2015-02-09 106 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

相关问题