Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
Android org.json.JSONException:无法将java.lang.String类型的值变量转换为JSONObject_Android_Json - Fatal编程技术网

Android org.json.JSONException:无法将java.lang.String类型的值变量转换为JSONObject

Android org.json.JSONException:无法将java.lang.String类型的值变量转换为JSONObject,android,json,Android,Json,我试图解析一个JSON,它显示了这个错误。我使用另一个JSON url测试了我的代码,它工作正常。下面是我的代码详细信息 网址- MainActivity.java 公共类主页扩展活动{ //发出请求的url 专用静态字符串url=”http://ads.mocean.mobi/ad?site=38206&zone=156431&type=2&size_x=57&size_y=57&count=5&ua=Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20

我试图解析一个JSON,它显示了这个错误。我使用另一个JSON url测试了我的代码,它工作正常。下面是我的代码详细信息

网址-

MainActivity.java
公共类主页扩展活动{
//发出请求的url
专用静态字符串url=”http://ads.mocean.mobi/ad?site=38206&zone=156431&type=2&size_x=57&size_y=57&count=5&ua=Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20AppleWebKit/537.22%20(KHTML,%20like%20Gecko)%20Chrome/25.0.1364.152%20Safari/537.22%20117.223.16.66&jsvar=test&type=2&key=6”;
//JSON节点名称
私有静态最终字符串APP_ARRAYNAME=“var test”;
私有静态最终字符串APP_URL=“URL”;
私有静态最终字符串APP_NAME=“text”;
私有静态最终字符串APP_IMAGE=“img”;
私有静态最终字符串图像\u TYPE=“TYPE”;
私有静态最终字符串APP_TRACK=“TRACK”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
setContentView(R.layout.home);
if(Utils.isNetworkAvailable(Home.this)){
新建MyTask().execute(url);
}否则{
showToast(“无网络连接!!!”;
}
}
类MyTask扩展了AsyncTask{
ProgressDialog;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(Home.this);
设置消息(“加载…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
返回Utils.getJSONString(参数[0]);
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
if(null!=pDialog&&pDialog.isShowing()){
pDialog.disclose();
}
if(null==result | | result.length()==0){
showToast(“从web上找不到数据!!!”;
//Home.this.finish();
}否则{
试一试{
JSONObject mainJson=新的JSONObject(结果);
//JSONArray JSONArray=mainJson.getJSONArray(数组名称);
JSONArray JSONArray=mainJson.getJSONArray(APP_ARRAYNAME);
for(int i=0;i
Utils.java
公共类Utils{
公共静态字符串getJSONString(字符串url){
字符串jsonString=null;
HttpURLConnection linkConnection=null;
试一试{
URL linkurl=新URL(URL);
linkConnection=(HttpURLConnection)linkurl.openConnection();
int responseCode=linkConnection.getResponseCode();
if(responseCode==HttpURLConnection.HTTP\u确定){
InputStream linkinStream=linkConnection.getInputStream();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
int j=0;
而((j=linkinStream.read())!=-1){
编写(j);
}
字节[]数据=baos.toByteArray();
jsonString=新字符串(数据);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
if(linkConnection!=null){
linkConnection.disconnect();
}
}
Log.i(“JSON响应----”,“”+jsonString);
返回jsonString;
}
公共静态布尔值isNetworkAvailable(活动){
ConnectionManager连接=(ConnectionManager)活动
.getSystemService(Context.CONNECTIVITY\u服务);
if(连接性==null){
返回false;
}否则{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null){
对于(int i=0;i
请为我提供一些解析这种JSON的解决方案

注意-:此Json在iPhone中经过良好测试,工作正常

带有JSON响应的Logcat详细信息
04-24 12:09:55.270:I/JSON响应--(2385):var test=[{“url”:”http://ads.mocean.mobi/2/redir/c5c61601-aca9-11e2-8400-a0369f167751/0/306865“,”文本“:”Instagram“,”img“:”http://img.ads.mocean.mobi/img/50e/cbf/01b1baf/image_57x57.png,“类型”:“图像/png”,“轨迹”:http://ads.mocean.mobi/2/img/c5c61601-aca9-11e2-8400-a0369f167751"},{“url”:”http://ads.mocean.mobi/2/redir/c5c61602-aca9-11e2-8400-a0369f167751/0/306834,“文本”:“Drawsomething”,“img”:http://img.ads.mocean.mobi/img/50e/cbe/2f70a73/image_57x57.png,“类型”:“图像/png”,“轨迹”:http://ads.mocean.mobi/2/img/c5c61602-aca9-11e2-8400-a0369f167751},{“url”:http://ads.mocean.mobi/2/redir/c5c61604-aca9-11e2-8400-a0369f167751
public class Home extends Activity {
    // url to make request
    private static String url = "http://ads.mocean.mobi/ad?site=38206&zone=156431&type=2&size_x=57&size_y=57&count=5&ua=Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20AppleWebKit/537.22%20(KHTML,%20like%20Gecko)%20Chrome/25.0.1364.152%20Safari/537.22%20117.223.16.66&jsvar=test&type=2&key=6";

    // JSON Node names
    private static final String APP_ARRAYNAME = "var test ";
    private static final String APP_URL = "url";
    private static final String APP_NAME = "text";
    private static final String APP_IMAGE = "img";
    private static final String IMAGE_TYPE = "type";
    private static final String APP_TRACK = "track";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.home);  
        if (Utils.isNetworkAvailable(Home.this)) {
            new MyTask().execute(url);
        } else {
            showToast("No Network Connection!!!");
        }
    }

    class MyTask extends AsyncTask<String, Void, String> {
        ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Home.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) { 
            return Utils.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }
            if (null == result || result.length() == 0) {
                showToast("No data found from web!!!");
                //Home.this.finish();
            } else {
                try {
                    JSONObject mainJson = new JSONObject(result);
                    //JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
                    JSONArray jsonArray = mainJson.getJSONArray(APP_ARRAYNAME);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                        // System.out.println(c.getInt(ID));
                          System.out.println(c.getString(APP_URL));
                          System.out.println(c.getString(APP_NAME));
                          System.out.println(c.getString(APP_IMAGE));
                        //  System.out.println(c.getInt(AGE));
                          System.out.println(c.getString(IMAGE_TYPE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void showToast(String msg) {
        Toast.makeText(Home.this, msg, Toast.LENGTH_LONG).show();
    }
}
public class Utils {
    public static String getJSONString(String url) {
        String jsonString = null;
        HttpURLConnection linkConnection = null;
        try {
            URL linkurl = new URL(url);
            linkConnection = (HttpURLConnection) linkurl.openConnection();
            int responseCode = linkConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream linkinStream = linkConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int j = 0;
                while ((j = linkinStream.read()) != -1) {
                    baos.write(j);
                }
                byte[] data = baos.toByteArray();
                jsonString = new String(data);  
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (linkConnection != null) {
                linkConnection.disconnect();
            }
        }
        Log.i("JSON Response----", ""+jsonString);
        return jsonString;
    }

    public static boolean isNetworkAvailable(Activity activity) {
        ConnectivityManager connectivity = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

}
  04-24 12:09:55.270: I/JSON Response----(2385): var test = [{"url" : "http://ads.mocean.mobi/2/redir/c5c61601-aca9-11e2-8400-a0369f167751/0/306865","text" : "Instagram","img" : "http://img.ads.mocean.mobi/img/50e/cbf/01b1baf/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61601-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61602-aca9-11e2-8400-a0369f167751/0/306834","text" : "Drawsomething","img" : "http://img.ads.mocean.mobi/img/50e/cbe/2f70a73/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61602-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61604-aca9-11e2-8400-a0369f167751/0/306518","text" : "Google Maps","img" : "http://img.ads.mocean.mobi/img/50e/c99/b95edc9/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61604-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61605-aca9-11e2-8400-a0369f167751/0/306765","text" : "Find Friends","img" : "http://img.ads.mocean.mobi/img/50e/cb5/ce11bc6/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61605-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c63d10-aca9-11e2-8400-a0369f167751/0/306733","text" : "Flashlight","img" : "http://img.ads.mocean.mobi/img/50e/cb3/c34c71a/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c63d10-aca9-11e2-8400-a0369f167751"}];
    04-24 12:09:55.290: W/System.err(2385): org.json.JSONException: Value var of type java.lang.String cannot be converted to JSONObject
    04-24 12:09:55.290: W/System.err(2385):     at org.json.JSON.typeMismatch(JSON.java:111)
    04-24 12:09:55.290: W/System.err(2385):     at org.json.JSONObject.<init>(JSONObject.java:158)
    04-24 12:09:55.301: W/System.err(2385):     at org.json.JSONObject.<init>(JSONObject.java:171)
    04-24 12:09:55.301: W/System.err(2385):     at com.impressol.appjiva.Home$MyTask.onPostExecute(Home.java:153)
    04-24 12:09:55.301: W/System.err(2385):     at com.impressol.appjiva.Home$MyTask.onPostExecute(Home.java:1)
    04-24 12:09:55.310: W/System.err(2385):     at android.os.AsyncTask.finish(AsyncTask.java:602)
    04-24 12:09:55.310: W/System.err(2385):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
    04-24 12:09:55.320: W/System.err(2385):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
    04-24 12:09:55.330: W/System.err(2385):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-24 12:09:55.330: W/System.err(2385):     at android.os.Looper.loop(Looper.java:137)
    04-24 12:09:55.330: W/System.err(2385):     at android.app.ActivityThread.main(ActivityThread.java:4340)
    04-24 12:09:55.330: W/System.err(2385):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-24 12:09:55.341: W/System.err(2385):     at java.lang.reflect.Method.invoke(Method.java:511)
    04-24 12:09:55.341: W/System.err(2385):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    04-24 12:09:55.341: W/System.err(2385):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    04-24 12:09:55.350: W/System.err(2385):     at dalvik.system.NativeStart.main(Native Method)
[
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a00e0-acab-11e2-afa2-a0369f06db33/0/306833",
        "text": "Amazon",
        "img": "http://img.ads.mocean.mobi/img/50e/cbb/ed021d2/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a00e0-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a00e2-acab-11e2-afa2-a0369f06db33/0/306864",
        "text": "Call of Duty",
        "img": "http://img.ads.mocean.mobi/img/50e/cbe/c0c7c88/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a00e2-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a27f0-acab-11e2-afa2-a0369f06db33/0/306868",
        "text": "GPS Drive",
        "img": "http://img.ads.mocean.mobi/img/50e/cc0/42a71e8/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a27f0-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a27f3-acab-11e2-afa2-a0369f06db33/0/306764",
        "text": "Twitter",
        "img": "http://img.ads.mocean.mobi/img/50e/cb5/942aaab/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a27f3-acab-11e2-afa2-a0369f06db33"
    },
    {
        "url": "http://ads.mocean.mobi/4/redir/a01a4f00-acab-11e2-afa2-a0369f06db33/0/306829",
        "text": "Temple run Brave",
        "img": "http://img.ads.mocean.mobi/img/50e/cba/c30b077/image_57x57.png",
        "type": "image/png",
        "track": "http://ads.mocean.mobi/4/img/a01a4f00-acab-11e2-afa2-a0369f06db33"
    }
]
                JSONObject mainJson = new JSONObject(result);
                //JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
                JSONArray jsonArray = mainJson.getJSONArray(APP_ARRAYNAME);
                JSONArray jsonArray = new JSONArray(result);
try {
                    JSONArray jsonArray = new JSONArray(result);


                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                          System.out.println(c.getString(APP_URL));
                          System.out.println(c.getString(APP_NAME));
                          System.out.println(c.getString(APP_IMAGE));
                          System.out.println(c.getString(IMAGE_TYPE));
                          System.out.println(c.getString(APP_TRACK));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }