Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.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时出现异常_Java_Android_Json - Fatal编程技术网

Java 处理JSON时出现异常

Java 处理JSON时出现异常,java,android,json,Java,Android,Json,每当我运行此代码时,总是会出现以下异常: Error parsing data org.json.JSONException: Value {"requestStatus":"0"} of type org.json. JSONObject cannot be converted to JSONArray. 我已经检查了其他答案,但它们对我不起作用 这是我的密码: import java.util.ArrayList; import org.apache.http.NameValuePai

每当我运行此代码时,总是会出现以下异常:

Error parsing data org.json.JSONException: Value {"requestStatus":"0"} 
of type org.json. JSONObject cannot be converted to JSONArray. 
我已经检查了其他答案,但它们对我不起作用

这是我的密码:

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.footballtesting.CustomHttpClient;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {

TextView txt_view1;
TextView txt_view3;
TextView txt_view2;
EditText txt_teamID;
ImageButton btn_save;
ImageButton btn_show;
RelativeLayout Relativelayoutrequest;
boolean haveTeamRequest;;//to store the result of mysql query after decoding with JSON


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt_view1=(TextView)findViewById(R.id.textView1);
    txt_view2=(TextView)findViewById(R.id.textView2);
    txt_view3=(TextView)findViewById(R.id.layout2textView1);
    txt_teamID=(EditText)findViewById(R.id.txt_teamID);
    btn_save=(ImageButton)findViewById(R.id.imgbtn_lsave);
    btn_show=(ImageButton)findViewById(R.id.imgbtn_layout2);
    Relativelayoutrequest=(RelativeLayout)findViewById(R.id.Relativelayoutrequest);
    checkOnCreate();
}
public void checkOnCreate()
{
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used     to       catch accidental disk or network access on the application's main thread
    .penaltyLog().build());
    Relativelayoutrequest=(RelativeLayout)findViewById(R.id.Relativelayoutrequest);
    String response=null;
    String st=null;
    String str=null;
    String memberID="000000011";
    String returnString=null;
    int status=1;
    //To pass to php
    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("userID",memberID));

    ArrayList<NameValuePair> pp = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("userID","409"));
    try {

     response = CustomHttpClient.executeHttpPost(
               "http://10.0.2.2/football365/checkRequestFromTeam.php",
          postParameters);//local host
        }
   catch (Exception e) {

        e.printStackTrace();
    }
    String result = response.toString();
    try{
        returnString = "";
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","requestStatus: "+json_data.getInt("requestStatus")
                );
                //Get an output to the screen
              status=json_data.getInt("requestStatus");
        }
}
catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
}



    Log.i("str", status+"JJJJJJJJJJJJJJJJ");
    Log.i("str", st+"BBBBBBBBBBBBBBBBBB");
    Log.i("str", str+"CCCCCCCCCCCCCCCCCCCCCCCCCC");


    }
}
您正在尝试对JSON对象使用JSONArray。 试一试


您是否在浏览器中查看您的回复??它是否返回json数组??此json:{requestStatus:0}是JSONObject而不是JSONArray。我想这是你的问题…错误在这行:JSONArray jArray=new JSONArrayresult;对不起,我不太懂安卓。但当我在logcat{requestStatus:0}Yes gilad hoch中输出响应时,它在log cat中显示如下。我怎样才能纠正它??
JSONObject cannot be converted to JSONArray. 
JSONObject obj = new JSONObject (result);