问题
After migrating to AndroidX,this code has stopped working. Before migrating to AndroidX,this code was working.
Error :
2020-02-24 09:41:17.918 17888-17921/edmt.dev.androidgridlayout E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, 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 file:
<?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>
回答1:
Add this line - android:usesCleartextTraffic="true"
under the application tag of your manifest file.
I have updated the code of your manifest file with the required statement.
<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>
来源:https://stackoverflow.com/questions/60369614/goldfishaddressspacehostmemoryallocator-ioctl-ping-failed