【问题标题】:Android Json parsing ErrorAndroid Json 解析错误
【发布时间】:2017-08-09 12:42:34
【问题描述】:

org.json.JSON 异常:输入在字符 0 处结束

json 解析数据,这是我获取数据的链接 https://api.myjson.com/bins/1d3ei7 。使用菜单中的按钮刷新。 在活动中没有显示。在活动中没有显示。在活动中没有显示。在活动中没有显示。在活动中没有显示。在活动中没有显示。在活动中没有显示。在活动中没有显示。在活动中没有显示activty .没有东西显示在 activty .没有东西显示在 activty .没有东西显示在 activty

here is java class




 import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ListView;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;

    public class MainActivity extends AppCompatActivity {

        private String TAG = "MTAG";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView listView=(ListView)findViewById(R.id.lvbio);




                //    new JsonTask().execute("https://api.myjson.com/bins/1d3ei7");


        }


       public  class JsonTask extends AsyncTask<String,String,List<Bio>>{
           @Override
           protected List<Bio> doInBackground(String... params) {
               HttpURLConnection connection=null;
               BufferedReader reader=null;
               try{

                   URL url=new URL(params[0]);
                  connection=(HttpURLConnection) url.openConnection();
                   connection.connect();
                   InputStream stream=connection.getInputStream();
                   reader=new BufferedReader(new InputStreamReader(stream));
                   StringBuffer buffer=new StringBuffer();
                   String line="";
                   while (reader.readLine()!=null){
                       buffer.append(line);
                   }
                   String finalJson=buffer.toString();


                   JSONObject parentObject=new JSONObject(finalJson);


                   JSONArray parentArray=parentObject.getJSONArray("Bio");
                   List<Bio> bioList=new ArrayList<>();

                   for (int i=0; i<parentArray.length();i++){
                       JSONObject finalobject=parentArray.getJSONObject(i);
                       Bio bio=new Bio();

                       bio.setId(finalobject.getInt("id"));
                       bio.setFirst_name(finalobject.getString("first_name"));
                       bio.setGender(finalobject.getString("gender"));
                       bio.setImage(finalobject.getString("image"));



                     bioList.add(bio);
                   }
                   return bioList;
               }
               catch (MalformedURLException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               } catch (JSONException e) {
                   e.printStackTrace();
               }
               return null;
           }

           @Override
           protected void onPostExecute(List<Bio> bios) {
               super.onPostExecute(bios);
           }
       }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main,menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
           // return super.onOptionsItemSelected(item);

            int id=item.getItemId();
            if (id==R.id.refresh){
                new JsonTask().execute("https://api.myjson.com/bins/1d3ei7");
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }



here is layout 




 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.offline.networking.MainActivity">


        <ListView
            android:id="@+id/lvbio"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

【问题讨论】:

  • 你得到了什么异常?post logcat
  • 您访问了错误的键名以进行图像检查一次
  • logcat 中没有显示任何内容,我想在列表中显示数据
  • 阅读here 了解如何发帖
  • 在调试时显示此错误:0 org.json.JSON 异常:在字符 0 处输入结束

标签: android json android-asynctask


【解决方案1】:

像这样解析你的 json:

String finalJson=buffer.toString();
   JSONArray array=new JSONArray(finalJson);
                    for(int i=0;i<array.length();i++){
                         Bio bio=new Bio();
                        JSONObject finalObject = array.getJSONObject(i);
                       bio.setId( finalObject.getInt("id"));
                        bio.setFirst_name(finalObject.getString("first_name"));
                        bio.setGender(finalObject.getString("gender"));
                        bio.setImage(finalObject.getString("photourl"));
bioList.add(bio);
                    }

希望对你有所帮助..

【讨论】:

  • for image key is "photourl"
  • 现在显示 Bio 不能应用于 bio.setId(finalObject.get("id"));
  • setId 的数据类型是什么?
  • setId 的数据类型是 int 这里是 class Bio int id;字符串名;字符串性别;字符串图片;
  • 使用 bio.setId(finalObject.getInt("id"));
【解决方案2】:

根据@Deepak Rana,答案解析是正确的,但在您的代码中字符串行中为空

找到下面的代码进行连接和解析

private static final String USER_AGENT = "Mozilla/5.0";

public  class JsonTask extends AsyncTask<String,String,List<Bio>> {
    @Override
    protected List<Bio> doInBackground(String... params) {
        HttpURLConnection connection=null;
        BufferedReader reader=null;
        try{

            URL url=new URL(params[0]);
            connection=(HttpURLConnection) url.openConnection();
            connection.connect();
            connection.setRequestMethod("GET");
            int responseCode = connection.getResponseCode();
            System.out.println("GET Response Code :: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK) { // success
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    getResponse = String.valueOf(response.append(inputLine));
                }
                in.close();

                // print result
                System.out.println(response.toString());
            } else {
                System.out.println("some error");
            }
            String finalJson=getResponse.toString();

            JSONArray parentArray=new JSONArray(finalJson);
            List<Bio> bioList=new ArrayList<>();

            for (int i=0; i<parentArray.length();i++){
                JSONObject finalobject=parentArray.getJSONObject(i);
                Bio bio=new Bio();

                bio.setId(finalobject.getInt("id"));
                bio.setFirst_name(finalobject.getString("first_name"));
                bio.setGender(finalobject.getString("gender"));
                bio.setImage(finalobject.getString("photourl"));

                bioList.add(bio);
            }
            return bioList;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多