所以我一直在尝试将包含base64编码图像的JSONObject发送到我用Flask制作的API,并且托管在测试网络服务器上。
String imgData = Base64.encodeToString(data, Base64.DEFAULT);
JSONObject jsonObj = new JSONObject();
try{
jsonObj.put("imgurl", imgData);
sendPost(jsonObj);
}
catch (JSONException e){
Context context2 = getApplicationContext();
CharSequence text2 = "Bye toast!";
int duration2 = Toast.LENGTH_SHORT;
Toast toast2 = Toast.makeText(context2, text2, duration2);
toast2.show();
}
try {
Context connTe = getApplicationContext();
CharSequence connectText = "Connection function successful";
int connectDur = Toast.LENGTH_SHORT;
Toast connectToast = Toast.makeText(connTe, connectText, connectDur);
connectToast.show();
URL url = new URL("http://BLABLABLA.pythonanywhere.com/image");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
Log.i("JSON", jsonObj.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonObj.toString());
os.flush();
os.close();
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG", conn.getResponseMessage());
conn.disconnect();
}
catch (Exception a) {
a.printStackTrace();
}
但是当我查看日志时,甚至没有任何活动。 api正常工作,因为我多次使用Postman测试它。
我缺少什么?
编辑: 那里的祝酒词只是随机想要看到一些功能相关的东西,将被删除。
!11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:android.os.NetworkOnMainThreadException 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303) 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86) 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at java.net.InetAddress.getAllByName(InetAddress.java:752) 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.Network $ 1.resolveInetAddresses(Network.java:29 ) 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java: 187) 11-07 12:26:24.665 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java: 156) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java: 98) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java: 345) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java: 328) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java: 246) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java: 457) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java: 126) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java: 257) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.google.android.gms.samples.vision.barcodereader.BarcodeCaptureActivity $ 3.onPictureTaken (BarcodeCaptureActivity.java:402) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.google.android.gms.samples.vision.barcodereader.ui.camera。 CameraSource $ PictureDoneCallback.onPictureTaken(CameraSource.java:697) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at android.hardware.Camera $ EventHandler.handleMessage(Camera.java:1611) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at android.os.Handler.dispatchMessage(Handler.java:102) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at android.os.Looper.loop(Looper.java:173) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at android.app.ActivityThread.main(ActivityThread.java:6459) 11-07 12:26:24.666 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at java.lang.reflect.Method.invoke(Native Method) 11-07 12:26:24.667 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java: 938) 11-07 12:26:24.667 6228-6228 / com.google.android.gms.samples.vision.barcodereader W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828) >!
修改
对于那些遇到同样问题的人,它与网络预设有关,我忘记提交申请。