您好,我想要一个来自 mysql 数据库的联系人列表(全名),我在 json 中收到错误。先感谢您
错误:json.JSONException:java.lang.String 类型的值 DOCTYPE 无法转换为 JSONArray
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.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
public class ListContact extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_contact);
new downloadTask().execute();
}
protected class downloadTask extends AsyncTask<String, String, ArrayList<Contact>>{
ArrayList<Contact> _myListContact;
Contact contact;
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(ListContact.this, "", " Loading ....");
}
@Override
protected ArrayList<Contact> doInBackground(String... jsonData) {
this._myListContact = new ArrayList<Contact>();
String resultat = null ;
InputStream is = null ;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost requete = new HttpPost("http://192.168.1.2/conta.php");
HttpResponse reponse = httpclient.execute(requete);
HttpEntity entity = reponse.getEntity();
is = entity.getContent();
} catch (Exception e){
Log.e("erreur log" , "erreur lors connextion htpp" +e.toString());
} try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is) , 8);
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null ){
builder.append(line + "/n");
} is.close();
resultat = builder.toString();
} catch (Exception e){
Log.e("erreur log" , "erreur lors du parsing " +e.toString());
}
try {
JSONArray _jArray = new JSONArray(resultat.toString());
for (int i = 0 ; i<_jArray.length(); i++) {
contact = new Contact();
contact.setFull_name(_jArray.getJSONObject(i).getString("full_name").toString());
_myListContact.add(contact);
}
} catch (Exception e){
Log.e("erreur log" , "erreur lors du parsing json" +e.toString());
}
return _myListContact;
}
@Override
protected void onPostExecute(ArrayList<Contact> _myListContact) {
list = (ListView)findViewById(R.id.listcontact);
AdapterContact adapter = new AdapterContact(ListContact.this, _myListContact);
list.setAdapter(adapter);
progressDialog.dismiss();
}
}
}