我正在从json api到我的数组列表中进行解析;
package com.example.dovizkuru;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public class DownloadDovizTask extends AsyncTask<String,Void,String>{
public interface GetDovizResponseCallback{
void dovizLoadingFinished(String response);
}
GetDovizResponseCallback getDovizResponseCallback;
public DownloadDovizTask(GetDovizResponseCallback getDovizResponseCallback) {
this.getDovizResponseCallback = getDovizResponseCallback;
}
@Override
protected String doInBackground(String... urls) {
Log.d("URL", urls[0]);
String url = urls[0];
String response = "";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
Log.d("URL", "RESPONSE: "+response);
return response;
}
@Override
protected void onPostExecute(String result) {
getDovizResponseCallback.dovizLoadingFinished(result);
}
}
然后我在像
这样的ArrayList中得到它@Override
public void dovizLoadingFinished(String response) {
// TODO Auto-generated method stub
ArrayList<String> list1=new ArrayList<String>();
JSONArray jsonArrayDoviz;
try {
JSONObject jObj = new JSONObject(response);
jsonArrayDoviz = jObj.getJSONArray("value");
dovizList.clear();
for (int i = 0; i < jsonArrayDoviz.length(); i++) {
Doviz doviz = new Doviz();
JSONObject jsonObject = jsonArrayDoviz.getJSONObject(i);
doviz.setAdi(jsonObject.getString("adi"));
doviz.setAlis(jsonObject.getString("alis"));
doviz.setSatis(jsonObject.getString("satis"));
doviz.setKey(jsonObject.getString("key"));
doviz.setOran(jsonObject.getString("oran"));
doviz.setType(jsonObject.getString("type"));
doviz.setUpDown(jsonObject.getString("upDown"));
list1.add(doviz.getAdi());
list1.add(doviz.getAlis());
list1.add(doviz.getSatis());
dovizList.add(doviz);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ListView listview =(ListView) findViewById(R.id.listView1);
adapter = new Adapter(this,dovizList);
listview.setAdapter(adapter);
}
然后在另一个类我试图获取数据,这是来自ArrayList的alis值,但它返回null / zero给我。
我尝试制作一个GetArraylist();首先获得arraylist的功能 我还尝试将ArrayList声明设为public static
顺便说一句,我尝试访问数据的新类是另一个意图。我的解析工作正常,我在第一个意图中列出它们。
所以我可以通过其他方式从这个ArrayList获取数据吗?
提前感谢您的帮助。
答案 0 :(得分:0)
找到解决方案,在列表中使用public static,然后在新类中获取它们。它以前没有用的原因是由于我使用的API的一些不好的反馈尝试了一个捕获,替换可能给我','而不是'的错误反馈。在某些地方,它的工作原理。