0

私はアンドロイドの新人です.Webサービスから値を取得する必要があります.ここではjson解析を使用しました.出力Json形式は{ "flag": 0 }です. ここでは、フラグの値を取得する必要があり、その値を使用して別のメソッドを開始します。フラグの値を取得するにはどうすればよいですか。私を助けてください。こんな感じで使いました。

public String objectvalue(String result){

    try {
        JSONObject obj=new JSONObject(result);

            String flag=obj.getString("flag");
            mString=flag;
            System.out.println(mString);


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    return mString;
}

しかし、私はフラグの値を取得しませんでした.Here引数結果はサーバーから出力されます.ie,result={ "flag": 0 }

4

4 に答える 4

0

まず、mString変数の宣言はどこですか?このメソッドを持つJavaクラスのように宣言されているinstance variableと思いますが、そうでない場合は確認してください。

次の形式の整形式の JSON があります。

{ "flag": 0 }

に変換するとJSONObject完全に機能するはずです。

flagキーの値を抽出するため

flagこれJSONObjectには値を持つという名前のキーがあり、メソッドIntegerを使用してそれを抽出しようとしていますgetString()

次のメソッド呼び出しのいずれかを使用する必要があります

optInt(文字列キー)

キーに関連付けられたオプションの int 値を取得します。そのようなキーがない場合、または値が数値でない場合はゼロを取得します。値が文字列の場合、数値として評価しようとします。

int flag = optInt("flag");

また

optInt(文字列キー, int defaultValue)

キーに関連付けられたオプションの int 値を取得します。そのようなキーがない場合、または値が数値でない場合はデフォルトを取得します。値が文字列の場合、数値として評価しようとします。

int flag = optInt("flag", 0);

変更を加えたコード

public int objectValue(String result){
    int flag = 0;
    try {
        JSONObject obj=new JSONObject(result);
        flag = obj.optInt("flag");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return flag;
}

編集:コメントの質問への回答。

public String objectValue(String result){
    int flag = 0;
    try {
        JSONObject obj=new JSONObject(result);
        flag = obj.optInt("flag");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return String.valueOf(flag);
}

お役に立てれば。

于 2014-01-03T05:03:23.687 に答える
0
{  // JSONObject
   "flag": 0 
}

使用する

JSONObject obj=new JSONObject(result);
int flag=obj.getInt("flag");

http://developer.android.com/reference/org/json/JSONObject.html

public int getInt (String name)

Added in API level 1
Returns the value mapped by name if it exists and is an int or can be coerced to an int.

Throws
JSONException   if the mapping doesn't exist or cannot be coerced to an int.

これが機能しない場合は、さらに情報を投稿する必要があります。

于 2014-01-03T04:45:04.223 に答える
0

あなたの懸念のために AsyncTask を使用してみてください。JSONデータを取得するための最良かつ好ましい方法です

このように AsyncTask を定義します..

new BussinessOwnerHttpAsyncTask().execute();

そしてあなたの AsyncTask クラス ..

class BussinessOwnerHttpAsyncTask extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        // Progress dialog code goes over here ..

        pDialog = new ProgressDialog(getParent());
        pDialog.setMessage("Please wait ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        //  Maintaining Shared preferences class for further...

        HttpClient httpclient = new DefaultHttpClient();

        String myUrl = Your_url_goes_over_here;


        String encodedURL = "";
        try {
            encodedURL = URLEncoder.encode(myUrl, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            URL url = new URL(encodedURL);
            Log.d("asca", ""+url);
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Log.i("url", city_name + "~" + country_name);
        Log.d("location", request_url+encodedURL);
        HttpGet httpget = new HttpGet(request_url+encodedURL);

        try {
            httpresponse = httpclient.execute(httpget);
            System.out.println("httpresponse" + httpresponse);
            Log.i("response", "Response" + httpresponse);
            InputStream is = httpresponse.getEntity().getContent();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();

            String recievingDataFromServer = null;
            while ((recievingDataFromServer = br.readLine()) != null) {
                Log.i("CHECK WHILE", "CHECK WHILE");
                sb.append(recievingDataFromServer);
            }

            myJsonString = sb.toString();
            Log.d("manish", myJsonString);
            serverSearchData = sb.toString();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return sb.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pDialog.dismiss();
        if (myJsonString.length() > 0) {
            try {
                myJsonObject = new JSONObject(myJsonString);

                String your_flag = myJsonObject.getString("flag");

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

    }

}

これで、クエリを実行できます..

于 2014-01-03T04:31:34.407 に答える