有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Volley在安卓中捕获Json字符串(使用dot net、newtonsoft制作)。

我需要在安卓应用程序中使用截击捕获一个json字符串(在我的例子中为:[“Java”、“C”、“C++”]),并将其放入字符串数组courseList中。 一些结束括号可能会丢失,很抱歉,我的文件中有更多的代码,但我无法完整复制它。请提供代码,如果你可以,这将b真的很有帮助。(谢谢) 以下是我的尝试代码:

import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.os.Bundle;

import 安卓.support.v7.app.AlertDialog;

import 安卓.util.Log;
import 安卓.view.View;
import 安卓.support.design.widget.NavigationView;
import 安卓.support.v4.view.GravityCompat;
import 安卓.support.v4.widget.DrawerLayout;
import 安卓.support.v7.app.ActionBarDrawerToggle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.AdapterView;
import 安卓.widget.ListView;
import 安卓.widget.Toast;

import com.安卓.volley.Request;
import com.安卓.volley.RequestQueue;
import com.安卓.volley.Response;
import com.安卓.volley.VolleyError;
import com.安卓.volley.toolbox.JsonObjectRequest;
import com.安卓.volley.toolbox.JsonRequest;
import com.安卓.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;



public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ListView simpleList;

        final String[] courseList = new String[3];// = {"HR","C Programming", "C++","C#", "Java", "Javascript", "Angular JS", "Data Structure", "Ajax", "Asp.Net", "jQuery", "json", "SQL"};
        int flags[] = {R.drawable.hr,R.drawable.cprogramming, R.drawable.cplus, R.drawable.csharp, R.drawable.java, R.drawable.javascript, R.drawable.angularjs, R.drawable.datastructures, R.drawable.ajax, R.drawable.aspnet, R.drawable.jquery, R.drawable.json, R.drawable.sql};

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // for volley
        String jsonURL = "http://192.168.0.131:10462/WebService1.asmx";
        final String data = "";
        RequestQueue requestQueue;

        requestQueue = Volley.newRequestQueue(this);
        JSONObject jsonRequest = new JSONObject();
        try {
            jsonRequest.put("GetTopic",Request.Method.GET );
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonURL,jsonRequest,
            new Response.Listener<JSONObject>() {

                // Takes the response from the JSON request
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject obj = response.getJSONObject("GetTopic");
                        // Retrieves the string labeled "colorName" and "description" from
                        //the response JSON Object
                        //and converts them into javascript objects
                        String color = obj.getString("GetTopic");
                       // Adds strings from object to the "data" string
                        String data = "Topic Name: " + color ;
                                // Adds the data string to the TextView "results"
                       // results.setText(data);
                        courseList[0] = data;
                        courseList[1] = data;
                        courseList[2] = data;

                       // Toast.makeText(getApplicationContext(),data,Toast.LENGTH_LONG).show();
                    }
                    // Try and catch are included to handle any errors due to JSON
                    catch (JSONException e) {
                        // If an error occurs, this prints the error to the log
                        e.printStackTrace();
                    }
                }
            },  new Response.ErrorListener() {
                @Override
                // Handles errors that occur due to Volley
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "Error");
                }
            });

        requestQueue.add(jsonObjectRequest);

        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), courseList, flags);
}

共 (0) 个答案