我想在我的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不道歉,如果我似乎愚蠢的傲慢和張貼張貼求助。我覺得所有想到的都耗盡了!
非常感謝您提前。
錯誤說明了一切:服務器的「json」響應是** NOT ** json。它是HTML。一個json響應不能以'<!DOCTYPE'開始...... – 2015-02-09 21:37:27