Android 将请求与改装不符';我无法通过,但curl通过了

Android 将请求与改装不符';我无法通过,但curl通过了,android,curl,flask,retrofit2,gunicorn,Android,Curl,Flask,Retrofit2,Gunicorn,我想从一个安卓应用程序向我的FlaskAPI发送一个PUT请求,该应用程序将对MongoDB文档进行更新。使用整个URL运行curl命令效果很好,但在运行改型函数时什么也没发生 查看服务器上的日志,我可以看到所有GET请求,但看不到PUT请求 flask | [2020-06-18 19:07:16 +0000] [19] [DEBUG] GET /beers/all flask | [2020-06-18 19:07:30 +0000] [20] [

我想从一个安卓应用程序向我的FlaskAPI发送一个PUT请求,该应用程序将对MongoDB文档进行更新。使用整个URL运行curl命令效果很好,但在运行改型函数时什么也没发生

查看服务器上的日志,我可以看到所有GET请求,但看不到PUT请求

flask | [2020-06-18 19:07:16 +0000] [19] [DEBUG] GET /beers/all flask | [2020-06-18 19:07:30 +0000] [20] [DEBUG] GET /beers/by_barcode flask | [2020-06-18 19:07:30 +0000] [10] [DEBUG] GET /beers/by_id
我正在编写一个OkHttpRequest调用来替换出现故障的改装调用,突然:

Android不允许在主线程上进行网络调用,只有在单独的线程或后台服务上进行同步调用,才能进行同步调用

阻止我打电话的原因是它是从另一个网络呼叫的主体发起的。使该问题难以跟踪的原因是完全没有错误日志。我应该在哪里找到对调试代码有用的提示

我最终将代码更改为:

fun onScanFinished(barcode: Long) {
    uiScope.launch {
        BeerApi.retrofitService.searchByBarcode(barcode).enqueue(object : Callback<BeerES> {
            override fun onFailure(call: Call<BeerES>, t: Throwable) {
                Toast.makeText(getApplication(), "Failure: " + t.message, Toast.LENGTH_LONG)
                    .show()
            }
            override fun onResponse(call: Call<BeerES>, response: Response<BeerES>) {
                var beerScanned = response.body()
                if (beerScanned != null) {
                    UserApi.retrofitService.addScanned(
                        FirebaseAuth.getInstance().currentUser!!.uid,
                        beerScanned.beerId,
                        beerScanned.beerInfo.beerName
                    )
                }
            }
        })
    }
}
fun onscannefinished(条形码:长){
发射{
ReformationService.searchByBarcode(条形码).enqueue(对象:回调){
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Toast.makeText(getApplication(),“Failure:+t.message,Toast.LENGTH\u LONG)
.show()
}
覆盖fun onResponse(调用:调用,响应:响应){
var beerscanding=response.body()
如果(啤酒罐装!=null){
UserApi.RefughtService.addScanned(
FirebaseAuth.getInstance().currentUser!!.uid,
啤酒罐头,
啤酒罐头
)
}
}
})
}
}
。。。对此(使用自己的uiScope在另一个方法中提取了第二个Api调用):

fun onscannefinished(条形码:长){
发射{
ReformationService.searchByBarcode(条形码).enqueue(对象:回调){
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Toast.makeText(getApplication(),“Failure:+t.message,Toast.LENGTH\u LONG)
.show()
}
覆盖fun onResponse(调用:调用,响应:响应){
var beerscanding=response.body()
如果(啤酒罐装!=null){
添加(罐装啤酒)
}
}
})
}
}
私人娱乐(啤酒罐头:啤酒){
发射{
UserApi.RefughtService.addScanned(
FirebaseAuth.getInstance().currentUser!!.uid,
啤酒罐头,
啤酒罐头
).enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Toast.makeText(getApplication(),“Failure:+t.message,Toast.LENGTH\u LONG)
.show()
}
覆盖fun onResponse(调用:调用,响应:响应){
}
})
}
}

希望这能有所帮助。

Edit:为curl命令添加了烧瓶日志。还显示了一个curl命令示例。可以用占位符替换敏感信息。添加了curl命令示例。 flask | [2020-06-18 19:26:49 +0000] [11] [DEBUG] PUT /users/add_scanned flask | [2020-06-18 19:26:49 +0000] [11] [INFO] Adding beer scanned flask | [2020-06-18 19:10:38 +0000] [20] [DEBUG] PUT /beers/update_barcode
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Query


private const val BASE_URL = "http://***.***.**.*:5000/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()

private val retrofit = Retrofit.Builder()
    .addConverterFactory(ScalarsConverterFactory.create())
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

interface UserApiService {

    /**
     * Adds a beer to scanned list
     */
    @PUT("users/add_scanned")
    fun addScanned(
        @Query("user_id") userId: String,
        @Query("beer_id") beerId: String,
        @Query("beer_name") beerName: String
        ):
            Call<String>
    }
from flask import Blueprint, jsonify, request, abort
from bson.objectid import ObjectId
from pymongo import MongoClient
from flask import current_app

bp = Blueprint('users', __name__, url_prefix='/users')
client = MongoClient(host="mongo", username="*****", password="*****")
db = client.TeddyMongo
collection = db.users

def to_dict(document):
    document["_id"] = str(document["_id"])
    return document

# When a user is not found
@bp.errorhandler(404)
def user_not_found(e):
    return jsonify(error=str(e)), 404

# Add beer scanned
@bp.route('/add_scanned', methods=['PUT'])
def add_scanned():
    user_id = request.args.get('user_id')

    scanned = {
        "_id" : request.args.get('beer_id'),
        "name" : request.args.get('beer_name')
    }

    if request.args.get('img_link'):
        scanned["img_link"] = request.args.get('img_link')

    current_app.logger.info("Adding beer scanned")

    collection.update_one({"_id":user_id}, { "$addToSet" : {"scanned" : scanned}}, upsert=True)

    return "Beer added to scanned"
fun onScanFinished(barcode: Long) {
    uiScope.launch {
        BeerApi.retrofitService.searchByBarcode(barcode).enqueue(object : Callback<BeerES> {
            override fun onFailure(call: Call<BeerES>, t: Throwable) {
                Toast.makeText(getApplication(), "Failure: " + t.message, Toast.LENGTH_LONG)
                    .show()
            }
            override fun onResponse(call: Call<BeerES>, response: Response<BeerES>) {
                var beerScanned = response.body()
                if (beerScanned != null) {
                    UserApi.retrofitService.addScanned(
                        FirebaseAuth.getInstance().currentUser!!.uid,
                        beerScanned.beerId,
                        beerScanned.beerInfo.beerName
                    )
                }
            }
        })
    }
}
fun onScanFinished(barcode: Long) {
    uiScope.launch {
        BeerApi.retrofitService.searchByBarcode(barcode).enqueue(object : Callback<BeerES> {
            override fun onFailure(call: Call<BeerES>, t: Throwable) {
                Toast.makeText(getApplication(), "Failure: " + t.message, Toast.LENGTH_LONG)
                    .show()
            }
            override fun onResponse(call: Call<BeerES>, response: Response<BeerES>) {
                var beerScanned = response.body()
                if (beerScanned != null) {
                    addScanned(beerScanned)
                }
            }
        })
    }
}

private fun addScanned(beerScanned: BeerES) {
    uiScope.launch {
        UserApi.retrofitService.addScanned(
            FirebaseAuth.getInstance().currentUser!!.uid,
            beerScanned.beerId,
            beerScanned.beerInfo.beerName
        ).enqueue(object :  Callback<String> {
            override fun onFailure(call: Call<String>, t: Throwable) {
                 Toast.makeText(getApplication(), "Failure: " + t.message, Toast.LENGTH_LONG)
                    .show()
            }
            override fun onResponse(call: Call<String>, response: Response<String>) {
            }
        })
    }
}