當我嘗試在吐司中顯示它時解析json數據,但arraylist保持爲空。我該如何解決這個問題?JSON解析但沒有存儲到想要的arraylist
這是的AsyncTask解析JSON
public class DownloadDetailImages extends AsyncTask<Void, Void, String> {
Context context;
ArrayList<String> arrayList = new ArrayList<>();
String urlAddress;
public DownloadDetailImages(Context context, ArrayList<String> arrayList, String urlAddress) {
this.context = context;
this.arrayList = arrayList;
this.urlAddress = urlAddress;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
try {
JSONArray jsonArray = new JSONArray(json);
JSONObject jsonObject;
arrayList.clear();
for (int i = 0; i < jsonArray.length(); i++)
{
jsonObject = jsonArray.getJSONObject(i);
String imageUrl = jsonObject.getString("ProductImage");
arrayList.add(imageUrl);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... params) {
HttpURLConnection httpURLConnection = Connector.connect(urlAddress);
try{
InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer jsonData = new StringBuffer();
while((line = bufferedReader.readLine()) != null)
{
jsonData.append(line+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return jsonData.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
的代碼,這是主要的活動
public class MainActivity extends AppCompatActivity implements BaseSliderView.OnSliderClickListener,
ViewPagerEx.OnPageChangeListener {
SliderLayout sliderLayout;
ArrayList<String> images = new ArrayList<String>();
String urlAddress = "http://10.10.90.70/slider/sliderImage.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sliderLayout = (SliderLayout) findViewById(R.id.slider);
new DownloadDetailImages(this, images, urlAddress).execute();
for (int i = 0; i < images.size(); i++) {
TextSliderView textSliderView = new TextSliderView(MainActivity.this);
textSliderView
.image(images.get(i).toString())
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
textSliderView.bundle(new Bundle());
sliderLayout.addSlider(textSliderView);
}
sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
sliderLayout.setCustomAnimation(new DescriptionAnimation());
sliderLayout.setDuration(3000);
sliderLayout.addOnPageChangeListener(this);
}
@Override
protected void onStop() {
sliderLayout.stopAutoCycle();
super.onStop();
}
@Override
public void onSliderClick(BaseSliderView slider) {
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Log.d("Slider Demo", "Page Changed: " + position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
這是我用來連接到web服務器 公共類連接器{
類public static HttpURLConnection connect(String urlAddress)
{
try {
URL url = new URL(urlAddress);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
//connection properties
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
return httpURLConnection;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
,這是JSON響應
[{
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/mejakerja.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/mejakerja.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/fax.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/fax.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/kursikerja.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/kursikerja.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/printer.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/printer.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/projector.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/projector.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/scanner.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/scanner.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/brankas.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/brankas.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/copier.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/copier.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/copier1.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/copier1.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/PC.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/PC.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/PC1.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/PC1.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/laptop1.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/laptop1.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/laptop2.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/laptop2.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/nikon.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/nikon.jpg"
}, {
"0": "http:\/\/10.10.90.70\/babex\/Gambar\/handycam.jpg",
"ProductImage": "http:\/\/10.10.90.70\/babex\/Gambar\/handycam.jpg"
}]
謝謝。
你會得到什麼異常?你有沒有得到JSON的迴應?您的JSON響應是否包含ProductImage密鑰? –
是的,嘗試瀏覽器上的PHP文件,它具有必需的字段。並且沒有例外 –
不在瀏覽器中,您是否通過在logcat中打印檢查了您的asynctask的後執行? –