<分区>
我一直在使用 ASYNC TASK 通过 REST API 将数据从我的应用发送或获取到数据库,反之亦然。我在我的几个项目(已完成)中使用的代码也是我在其他新项目中使用的代码。我现在创建了一个新项目并且工作的异步代码不再获取数据,但是我在之前的任何项目中粘贴的相同代码项目,效果很好。我有一个明天开始的新项目,我希望我易于使用的代码在新项目中也能很好地工作。被认为是在 list 中添加了互联网许可.. MainActivity.java 中的代码应该在 postExecute 上显示来自数据库的 Toast
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String name;
ArrayList<String> NAME=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getpostcount().execute();
}
public class getpostcount extends AsyncTask<String, String, String> {
String db_url;
@Override
protected void onPreExecute() {
// Toast.makeText(newsFeed.this, "Loading........", Toast.LENGTH_SHORT).show();
db_url = "http://testprasis.000webhostapp.com/getshopname.php";
}
@Override
protected String doInBackground(String... args) {
try {
URL url = new URL(db_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
StringBuffer buffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
String data = stringBuilder.toString().trim();
String json;
InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
int size = stream.available();
byte[] buffer1 = new byte[size];
stream.read(buffer1);
stream.close();
json = new String(buffer1, "UTF-8");
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
if (jsonObject.getString("id") != null) {
name = jsonObject.getString("shop_name");
NAME.add(name);
}
}
return null;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
//
return null;
}
@Override
protected void onPostExecute(String s) {
Toast.makeText(MainActivity.this,NAME.get(0), Toast.LENGTH_SHORT).show();
}
}
}
洛卡特:
2019-11-27 17:13:00.044 26790-26790/com.sheershakx.hisab E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sheershakx.hisab, PID: 26790
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.sheershakx.hisab.MainActivity$getpostcount.onPostExecute(MainActivity.java:119)
at com.sheershakx.hisab.MainActivity$getpostcount.onPostExecute(MainActivity.java:38)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)