我并不很清楚你的查询是什么,但我会给出怎样的数据可以从服务器调用到移动应用程序的一些见解。 您可以使用AsyncTask在后台获取数据,而不会影响您的主线程。
调用的AsyncTask在YOUT onCreate方法
new getDataList().execute();
如:
public class getDataList extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
protected void onPreExecute(){
}
@Override
protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
// Locate the WorldhavingDelay Class
// JSON file URL address
ArrayList<String> result = new ArrayList<String>();
//52.77.68.231
ncardsmore.clear();
try {
jsonobject = JSONfunctions
.getJSONfromURL("http://52.42.251.70/testData/test2.php?language=" + myLanguage);
Log.e("JSONARRAY", jsonobject.toString());
// Locate the NodeList name
if (jsonobject != null){
jsonarray = jsonobject.getJSONArray("news");
for (int i = 0; i < jsonarray.length(); i++) {
if (isCancelled())
break;
String myId = Integer.toString(i);
jsonobject = jsonarray.getJSONObject(i);
exist = jsonobject.optString("exist");
if (exist.equals("true")) {
id = jsonobject.optString("id");
imageUrl = jsonobject.optString("imageUrl");
myHeader = jsonobject.optString("header");
description = jsonobject.optString("description");
//mydate = jsonobject.optString("date");
mydate = "12/12/12";
//adding to database
mydb.addNews(new NewsData(id, myHeader, description, imageUrl, myId));
result.add("exist");
} else {
result.add("notExist");
}
}
}
else{
result.add("error");
}
} catch (Exception e) {
}
return result;
}
protected void onPostExecute(ArrayList<String> result) {
String existornot = result.get(0);
//Use your code here
}
创建getJSONfromURL方法单独的类(即JSONfunctions)
import android.util.Log;
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.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
String defaultVal = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
jArray = null;
Log.e("log_tag", "Error in http connection " + e.toString());
// return err;
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
jArray = null;
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
我希望你得到它。
啊,这样的叫法,我将寻找更多的成 – Jackelll