我已经开发了HTTP Post和Get方法。首先,Http post方法清楚地工作并且身份验证成功但第二步我想获取数据信息,我想要选择Jason对象返回null。
遵循主要代码:
@Override
protected Void doInBackground(Void... unused) {
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("login","userno"));
params.add(new BasicNameValuePair("password", "pass"));
params.add(new BasicNameValuePair("Accept","application/json"));
params.add(new BasicNameValuePair("Content-type","x-www-form-urlencoded; charset=UTF-8"));
veri_string=post.httpPost(url, "GET", params, 2000);
try {
veri_json=new JSONObject(veri_string);
} catch (Exception e) {
e.printStackTrace();
}
Log.d("HTTP POST CEVAP",""+veri_json);
return null;
}
我的邮政课程代码:
try {
if (method == "POST") {
HttpParams httpParameters = new BasicHttpParams();
int timeout1 = time;
int timeout2 = time;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
veri = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
veri = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
veri, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
veri.close();
veri_string = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Hata " + e.toString());
}
return veri_string; // Aldığımız cevabın string halini geri dönüyoruz
MY web api url:http://ogrenci.izmir.edu.tr/api/ogrenciders
{ “ID”:2602, “OgrenciNo”: “”, “OgrenciAdiSoyadi”:NULL, “OgretimYili”:2014, “OgretimDonemi”:1, “AldigiDersIdNo”:0.0 “AldigiSube”:NULL, “OgretimYiliAdi” :“2014-2015”,“OgretimDonemiAdi”:“GüzDönemi”,“DersKodu”:“CEN 431”,“DersAdi”:“VERİİLETİŞİMİ”,“Sinif”:4,“SinifAdi”:“4.sınıf”, “Vize1”:0 “Vize2”:0 “Vize3”:0 “Vize4”:0 “Vize5”:0 “Vize6”:0 “Vize7”:0 “Vize8”:0,“Vize9 “:0,” Vize10 “:0,” 最终 “:0”,Butunleme “:0”,EkSinav “:0”,Vize1Adi “:” 维泽 70" , “Vize2Adi”:“测验 64" , “Vize3Adi”:“分配
最后我就是这样的错误
答案 0 :(得分:0)
在你的Asynctask doInBackground方法中,你必须返回你想要获得的东西。在您的情况下,您需要json字符串
try {
veri_json=new JSONObject(veri_string);
}
目前你正在返回null,所以这个变量永远不会传回给调用者类。
此外,您还希望在&#34; onPostExecute&#34;中返回此变量。方法。所以doInBackground返回一些东西(一个String),然后onPostExecute接收字符串(它自动,因为它们是Asynctask的Override方法),然后相同的onPostExecute方法将结果(接收到的String)返回给调用者类,通常是接口