Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取特定json值http post android studio_Java_Android_Json_Http Post - Fatal编程技术网

Java 获取特定json值http post android studio

Java 获取特定json值http post android studio,java,android,json,http-post,Java,Android,Json,Http Post,我是android开发的初学者。我已经上传了文件,并从服务器得到了响应。但是,响应包含我不想要的值。 服务器响应为:Value{“time_used”:53840,“result_idcard”:{“index1”:0,“index2”:0,“confidence”:87.42464,”} 我只想要置信度。我如何提取它?当我运行下面的代码时,logcat显示: 错误:类型为org.json.JSONObject的org.json.JSONException无法转换为JSONArray 请帮帮我

我是android开发的初学者。我已经上传了文件,并从服务器得到了响应。但是,响应包含我不想要的值。 服务器响应为:Value{“time_used”:53840,“result_idcard”:{“index1”:0,“index2”:0,“confidence”:87.42464,”}

我只想要置信度。我如何提取它?当我运行下面的代码时,logcat显示:

错误:类型为org.json.JSONObject的org.json.JSONException无法转换为JSONArray

请帮帮我

/** *将文件上载到服务器 */

私有类UploadFileToServer扩展异步任务{
字符串docPath=null;
字符串facePath=null;
public UploadFileToServer(String docPath、String facePath)抛出JSONException{
this.docPath=docPath;
this.facePath=facePath;
}
@凌驾
受保护的void onPreExecute(){
//将进度条设置为零
progressBar.setProgress(0);
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
//使进度条可见
progressBar.setVisibility(View.VISIBLE);
//更新进度条值
progressBar.setProgress(progress[0]);
//更新百分比值
txtPercentage.setText(String.valueOf(progress[0])+“%”;
//在通知栏中显示进度的代码
FileUploadNotification FileUploadNotification=新FileUploadNotification(UploadActivity.this);
fileUploadNotification.updateNotification(String.valueOf(progress[0]),“Image 123.jpg”,“Camera Upload”);
}
@凌驾
受保护字符串doInBackground(无效…参数){
返回上传文件();
}
@抑制警告(“弃用”)
公共字符串上载文件(){
字符串responseString=null;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(Config.FILE\u UPLOAD\u URL);
试一试{
AndroidMultiPartEntity=新AndroidMultiPartEntity(
新ProgressListener(){
@凌驾
已转移的公共无效(长数值){
出版进度((int)((num/(float)totalSize)*100);
}
});
addPart(“imageIdCard”,新文件体(新文件(docPath));
addPart(“imageBest”,新文件体(新文件(facePath));
totalSize=entity.getContentLength();
httppost.setEntity(实体);
//进行服务器呼叫
HttpResponse response=httpclient.execute(httppost);
HttpEntity r_entity=response.getEntity();
int statusCode=response.getStatusLine().getStatusCode();
如果(状态代码==200){
//服务器响应
responseString=EntityUtils.toString(r_实体);
}否则{
responseString=“发生错误!Http状态代码:”
+状态码;
}
}捕获(客户端协议例外e){
responseString=e.toString();
}捕获(IOE异常){
responseString=e.toString();
}
回报率;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//super.onPostExecute(结果);
//如果(结果!=null)
尝试
{
//将响应字符串转换为Json数组
JSONArray ja=新JSONArray(结果);
//遍历并检索俱乐部字段
int n=ja.length();
对于(int i=0;i

您正在将JSON结果转换为
JSONArray
,但结果只是一个对象。因此,直接将其解析为对象并获得所需的节点。而且,
结果\u idcard
是对象,您还需要将其转换为
JSONObject
然后获得
置信度
节点

试试这个:

@Override
protected void onPostExecute(String result) {
     try {
        JSONObject jsonObject = new JSONObject(result);

        //Retrieve each Json object's fields
        JSONObject request_id = jsonObject.getJSONObject("result_idcard");
        Double confidence = request_id.getDouble("confidence");

        showAlert(confidence);
     } catch (JSONException e) {
        e.printStackTrace();
     }
}

基于OP的问题(到目前为止)和OP提供的(无效的)JSON示例,我为他们做了一个小测试,也许OP会知道这是如何工作的

只需将此代码放在活动中的某个位置并调用
startJsonTest();
。您将在logcat中看到响应

private void startJsonTest(){
    // The JSON the OP provide in their question!
    String json = "{'time_use':53840,'result_idcard':{'index1':0,'index2':0,'confidence':87.42464}}";
    testYourJson(json);
}

private void testYourJson(String result) {
    try {
        if(result == null || result.isEmpty()){
            Log.e("testYourJson", "Something went wrong!");
            return;
        }

        Log.e("testYourJson", result);

        JSONObject jsonObject = new JSONObject(result);
        //Retrieve each Json object's fields
        int time = jsonObject.optInt("time_use", -1);
        Log.e("testYourJson", "time = " + time);
        JSONObject request_id = jsonObject.getJSONObject("result_idcard");

        Double confidence = request_id.optDouble("confidence", -222.0f);
        int index1 = request_id.optInt("index1", -1);
        int index2 = request_id.optInt("index2", -1);

        // Show a little confidence ;-)
        Log.e("testYourJson", "confidence  = " + confidence);
        Log.e("testYourJson", "index1  = " + index1);
        Log.e("testYourJson", "index2  = " + index2);
    } catch (JSONException e) {
        Log.e("testYourJson", e.getMessage());
    }
}
tententen的解决方案(这是正确的)唯一不同之处在于我使用了
optInt
optDouble
,因为您可以替换可选值

这是有效的!我已经测试过了。但是我怀疑你的JSON与你提供的不同。 祝你好运

编辑
在仔细查看屏幕截图之后,OP将问题链接到了他的问题上,看起来好像
index1
index2
实际上是
两个
值!因此实际工作的代码需要对此进行补偿!

服务器的响应仍然与之前相同。它仍然是
private void startJsonTest(){
    // The JSON the OP provide in their question!
    String json = "{'time_use':53840,'result_idcard':{'index1':0,'index2':0,'confidence':87.42464}}";
    testYourJson(json);
}

private void testYourJson(String result) {
    try {
        if(result == null || result.isEmpty()){
            Log.e("testYourJson", "Something went wrong!");
            return;
        }

        Log.e("testYourJson", result);

        JSONObject jsonObject = new JSONObject(result);
        //Retrieve each Json object's fields
        int time = jsonObject.optInt("time_use", -1);
        Log.e("testYourJson", "time = " + time);
        JSONObject request_id = jsonObject.getJSONObject("result_idcard");

        Double confidence = request_id.optDouble("confidence", -222.0f);
        int index1 = request_id.optInt("index1", -1);
        int index2 = request_id.optInt("index2", -1);

        // Show a little confidence ;-)
        Log.e("testYourJson", "confidence  = " + confidence);
        Log.e("testYourJson", "index1  = " + index1);
        Log.e("testYourJson", "index2  = " + index2);
    } catch (JSONException e) {
        Log.e("testYourJson", e.getMessage());
    }
}