Android工作室json处理json响应 [英] Android studio json handle json response

查看:77
本文介绍了Android工作室json处理json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Asp.net Api让Json回复像这样{\UserID \:5,\UserName \:\asd \,\\ \\ Password\:\ asd\,\ Email\:\ SS @ asd\,\ PhoneNumber\:\ 1213\,\\ \\Logtit \:0.0,\Latitle \:0.0,\OfGroup \:\a \}

Haw我可以处理这个Json Android Studio中的响应使用Volley Lib这个我的代码





我尝试了什么:



< pre> public  class  MainActivity 扩展 AppCompatActivity {


RequestQueue requestqueue;
按钮开始;
TextView textView;
EditText ee;
public String givenValue = ee.getText()。toString();
public String URL = http:// localhost:61511:8010 / api / Feedback / 5;
@覆盖
受保护 void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestqueue = Volley.newRequestQueue( this );
final 按钮开始=(按钮)findViewById(R.id.btnget);
final TextView textView =(TextView)findViewById(R.id.mTextView);
final EditText ee =(EditText)findViewById(R.id.idtxt);
start.setOnClickListener( new View.OnClickListener(){
@ Override
public void onClick(查看v){
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,URL,null, new Response.Listener< JSONObject>(){
@覆盖
public void onResponse(JSONObject响应){
尝试 {
JSONArray jsonArray = response.getJSONArray( 用户);
for 在t i = 0 ; i< jsonArray.length(); i ++){
JSONObject User = jsonArray.getJSONObject(i);

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

},
new Response.ErrorListener(){
< span class =code-sdkkeyword> @ Override
public void onErrorResponse (VolleyError错误){
Log.e( Volley ERROR);
}
}
);
requestqueue.add(jsonObjectRequest);}
});
}

解决方案

Volley只是一个以管理方式管理网络流量的界面,它不会做太多为您解析并生成响应。您需要使用该库来解析JSON- 在Android Developer上查看此链接 [ ^ ]网站,了解他们如何展示此 - 至于SQLite,Android还有另一个神奇的工具,房间数据库ORM,通过这种方式,你将使用Google的所有内容,首先是Volley的请求管理,然后是Gson,然后是最后一个Room来保存数据库中的数据。



我建议您使用Gson将此String解析为对象。但是,这还要求您首先创建一个与该JSON文档的签名匹配的类,以下是您所需要的,

 / * 
{UserID \\ \\ :5,\ UserName\ :\ asd\ \ Password\:\ asd\,\ Email\:\ SS @ asd\,\ PhoneNumber\:\ 1213\,\ Logtit\:0.0,\ Latitle\:0.0,\ OfGroup\:\\ \\a \}
* /
公共类用户{
//我不会进行封装,您可以将这些设置为私有
//并创建getter / setters。
public int UserId;
public String UserName;
public String Password;
public String Email;
public String PhoneNumber;
public double Logtit;
public double Latitle;
public String OfGroup;
}

现在你可以继续使用Gson将JSON文档解析为<$ c的对象$ c>用户类型。

 GsonBuilder bui lder =  new  GsonBuilder(); 
Gson gson = builder.create();

// 将JSON文档捕获为变量,我不知道你在哪里以及如何做到这一点。
用户user = gson。 fromJson(jsonString,User。 class );

这样,您可以将JSON转换为运行时对象。请参阅此处以获取此示例, http://tutorials.jenkov.com/java-json/gson.html [ ^ ]



GitHub - google / gson:一个Java序列化/反序列化库,用于将Java对象转换为JSON并返回 [ ^ ]

房间持久性库  |  Android开发者 [ ^ ]


i have Asp.net Api to get Json Response Like This "{\"UserID\":5,\"UserName\":\"asd\",\"Password\":\"asd\",\"Email\":\"ss@asd\",\"PhoneNumber\":\"1213\",\"Logtit\":0.0,\"Latitle\":0.0,\"OfGroup\":\"a \"}"

Haw can i handle this Json response in Android Studio Using Volley Lib This My Code



What I have tried:

<pre>public class MainActivity extends AppCompatActivity {


RequestQueue  requestqueue;
Button start;
TextView textView;
EditText ee;
public String givenValue = ee.getText().toString();
public String URL = "http://localhost:61511:8010/api/Feedback/5" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestqueue=Volley.newRequestQueue(this);
   final Button start =(Button)findViewById(R.id.btnget);
   final TextView textView =(TextView)findViewById(R.id.mTextView);
   final EditText ee =(EditText)findViewById(R.id.idtxt) ;
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("User");
                        for(int i= 0 ;i < jsonArray.length();i++){
                            JSONObject User = jsonArray.getJSONObject(i);

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

            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "ERROR");
                        }
                    }
            );
            requestqueue.add(jsonObjectRequest);}
    });
}

解决方案

Volley is just an interface to manage the network traffic in a managed way, it won't do much for you to parse and generate the response. You would need to use the library to parse the JSON—see this link on Android Developer[^] website to see how they are demonstrating this—and as for SQLite, Android has another amazing tool, Room database ORM, in this way, you will be using everything by Google starting with request management by Volley, then Gson and then lastly Room to persist the data in the database.

I recommend that you use Gson to parse this String to objects. However this also requires that you first create a class that matches the signature of that JSON document, the following is what you need,

/* 
{"UserID\":5,\"UserName\":\"asd\",\"Password\":\"asd\",\"Email\":\"ss@asd\",\"PhoneNumber\":\"1213\",\"Logtit\":0.0,\"Latitle\":0.0,\"OfGroup\":\"a \"}"
*/
public class User {
    // I won't go in encapsulation, you can set these to private
    // and create getter/setters.
    public int UserId;
    public String UserName;
    public String Password;
    public String Email;
    public String PhoneNumber;
    public double Logtit;
    public double Latitle;
    public String OfGroup;
}

Now you can go ahead and use Gson to parse the JSON document as an object of User type.

GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

// Capture the JSON document as a variable, I do not know where and how you do that.
User user = gson.fromJson(jsonString, User.class);

This way, you can convert the JSON to a runtime object. See here for an example of this, http://tutorials.jenkov.com/java-json/gson.html[^]

GitHub - google/gson: A Java serialization/deserialization library to convert Java Objects into JSON and back[^]
Room Persistence Library  |  Android Developers[^]


这篇关于Android工作室json处理json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆