私はあなたのクエリが何であるかはっきりしていませんが、私はサーバーからモバイルアプリケーションにデータを呼び出す方法についていくつかの洞察を与えます。 AsyncTaskを使用すると、メインスレッドに影響を与えずに、バックグラウンドでデータを取得できます。
コールのyoutのonCreateメソッド
new getDataList().execute();
例:中asyncTask:
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