我一直在使用ASYNC TASK来通过REST API将数据从应用程序发送或获取到数据库,反之亦然。我在少数几个新项目中使用的代码(已完成)也是我在其他新项目中使用的代码。我现在创建了一个新项目,并且工作中的异步代码不再获取数据,而是粘贴在以前的任何代码中项目,效果很好。我有一个明天开始的新项目,我希望我易于使用的代码在新项目中也能很好地工作。被认为是在清单中添加了Internet权限。 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();
}
}
}
LOGCAT:
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)
答案 0 :(得分:1)
为什么突然不起作用?
实际上它可以工作,但是在Android版本上低于Android P
的情况下。
因为默认情况下Android P
使用HTTPS
。这意味着,如果您在应用中使用未加密的HTTP
请求,则该应用将在除Android P
之外的所有Android版本(较低版本)中正常运行。
如何使其在Android P上运行?
network_security_config
文件,然后按以下方式将其包含在清单中即可。 network_security_config
文件如下所示。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">nikit19.github.io</domain>
</domain-config>
</network-security-config>
在清单中添加为
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config"
... >
...
</application>
</manifest>
仅在清单中的应用程序中使用此
android:usesCleartextTraffic="true"
答案 1 :(得分:0)
我认为问题解决了几天,我一直在胡闹:
我只是忘了在清单文件中添加android:usesCleartextTraffic="true"
。现在效果很好
由于它没有显示在我的logcat错误中,所以我一直在自欺欺人。
谢谢大家的评论。