迁移到AndroidX后,此代码已停止工作。
在迁移到AndroidX之前,此代码已正常工作。
错误:
2020-02-24 09:41:17.918 17888-17921 / edmt.dev.androidgridlayout E / eglCodecCommon:GoldfishAddressSpaceHostMemoryAllocator:device_type = 5的ioctl_ping失败,ret = -1
BackgroundTask.java:
package edmt.dev.androidgridlayout;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class BackgroundTask extends AsyncTask<String,String,String> {
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx=ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
// String url="http://192.168.42.159/kamakshi/addproduct.php?name="+name+"&quantity="+quantity;
String url="http://10.0.2.2/kamakshi/addproduct.php";
String method = params[0];
if (method.equals("addproduct"))
{
String name=params[1];
String quantity=params[2];
String cp=params[3];
String sp=params[4];
String etseller=params[5];
String seller=params[6];
String brand=params[7];
String colour=params[8];
String fabric=params[9];
String size=params[10];
String type=params[11];
// String gender=params[12];
System.out.println(method+"omkar"+name+"......"+quantity+" "+cp+" "+sp+" "+etseller+" "+seller+" "+brand+" "+colour+" "+fabric+" "+size);
try{
URL url1 = new URL(url);
HttpURLConnection httpsURLConnection = (HttpURLConnection)url1.openConnection();
if(httpsURLConnection!=null)
{
System.out.println("connn established");
}
else
{
System.out.println("connn failed");
}
System.out.println("connn established");
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setDoOutput(true);
OutputStream os=httpsURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("cp","UTF-8")+"="+URLEncoder.encode(cp,"UTF-8")+"&"+
URLEncoder.encode("sp","UTF-8")+"="+URLEncoder.encode(sp,"UTF-8")+"&"+
URLEncoder.encode("etseller","UTF-8")+"="+URLEncoder.encode(etseller,"UTF-8")+"&"+
URLEncoder.encode("seller","UTF-8")+"="+URLEncoder.encode(seller,"UTF-8")+"&"+
URLEncoder.encode("brand","UTF-8")+"="+URLEncoder.encode(brand,"UTF-8")+"&"+
URLEncoder.encode("colour","UTF-8")+"="+URLEncoder.encode(colour,"UTF-8")+"&"+
URLEncoder.encode("fabric","UTF-8")+"="+URLEncoder.encode(fabric,"UTF-8")+"&"+
URLEncoder.encode("size","UTF-8")+"="+URLEncoder.encode(size,"UTF-8")+"&"+
URLEncoder.encode("type","UTF-8")+"="+URLEncoder.encode(type,"UTF-8")+"&"+
// URLEncoder.encode("gender","UTF-8")+"="+URLEncoder.encode(gender,"UTF-8")+"&"+
URLEncoder.encode("quantity","UTF-8")+"="+URLEncoder.encode(quantity,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream IS= httpsURLConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(IS,"iso-8859-1"));
br.close();
IS.close();
httpsURLConnection.disconnect();
}catch (Exception e)
{
e.printStackTrace();
}
}
return url;
}
}
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidgridlayout">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".scandemo"></activity>
<activity android:name=".Scanner" />
<activity android:name=".ProductTable" />
<activity android:name=".ProductView" />
<activity android:name=".addproduct" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityOne" />
</application>
</manifest>
最佳答案
在清单文件的应用程序标记下添加此行-android:usesCleartextTraffic="true"
。
我已使用所需的语句更新了清单文件的代码。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edmt.dev.androidgridlayout">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".scandemo"></activity>
<activity android:name=".Scanner" />
<activity android:name=".ProductTable" />
<activity android:name=".ProductView" />
<activity android:name=".addproduct" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityOne" />
</application>
</manifest>