在Android应用程序中使用json输出显示时我的Java文件出错

时间:2014-02-01 11:29:44

标签: android json

以下是我的Main.java文件的代码:

package com.exampe.jsonoutput;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {

Button b;

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

    initialization();

}

private void initialization() {

    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View arg0) {

    try{

        HttpParams httpparams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpparams, 10000);
        HttpConnectionParams.setSoTimeout(httpparams, 10000);

        HttpParams p = new BasicHttpParams();
        p.setParameter("Id", 1);

        HttpClient hc = new DefaultHttpClient();
        String url = "http://localhost/Android%20Output/fetch.php";

        HttpPost httppost = new HttpPost(url);

        try{

            Log.i(getClass().getSimpleName(), "Send task - Start");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("Id",1));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = hc.execute(httppost,
                    responseHandler);
            // Parse
            JSONObject json = new JSONObject(responseBody);
            JSONArray jArray = json.getJSONArray("posts");
            ArrayList<HashMap<String, String>> mylist = 
                   new ArrayList<HashMap<String, String>>();

            for (int i = 0; i < jArray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = jArray.getJSONObject(i);
                String s = e.getString("post");
                JSONObject jObject = new JSONObject(s);

                map.put("Id", jObject.getString("Id"));
                map.put("First Name", jObject.getString("F_Name"));
                map.put("Middle Name", jObject.getString("M_Name"));
                map.put("Last Name", jObject.getString("L_Name"));

                mylist.add(map);
            }
            Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();

        } catch (ClientProtocolException e) {

            e.printStackTrace();

        } 
        catch (IOException e) {

            e.printStackTrace();

        }
        // Log.i(getClass().getSimpleName(), "send  task - end");

    } catch (Throwable t) {
        Toast.makeText(this, "Request failed: " + t.toString(),
                Toast.LENGTH_LONG).show();
    }

}

}

该行

nameValuePairs.add(new BasicNameValuePair("Id",1));

导致错误消息The constructor BasicNameValuePair(String, int) is undefined

如何解决?

2 个答案:

答案 0 :(得分:1)

您可以像这样使用asyncTask

@Override
public void onClick(View arg0) {

    new NetworkThread().execute();
}

public class NetworkThread extends AsyncTask<Void, Void, String>
{

    @Override
    protected String doInBackground(Void... params) {
         String responseBody = "";
        try{

            HttpParams httpparams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpparams, 10000);
            HttpConnectionParams.setSoTimeout(httpparams, 10000);

            HttpParams p = new BasicHttpParams();
            p.setParameter("Id", 1);

            HttpClient hc = new DefaultHttpClient();
            String url = "http://localhost/Android%20Output/fetch.php";

            HttpPost httppost = new HttpPost(url);

            try{

                Log.i(getClass().getSimpleName(), "Send task - Start");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("Id","1"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                responseBody = hc.execute(httppost,
                        responseHandler);
                // Parse
                JSONObject json = new JSONObject(responseBody);
                JSONArray jArray = json.getJSONArray("posts");
                ArrayList<HashMap<String, String>> mylist = 
                       new ArrayList<HashMap<String, String>>();

                for (int i = 0; i < jArray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject e = jArray.getJSONObject(i);
                    String s = e.getString("post");
                    JSONObject jObject = new JSONObject(s);

                    map.put("Id", jObject.getString("Id"));
                    map.put("First Name", jObject.getString("F_Name"));
                    map.put("Middle Name", jObject.getString("M_Name"));
                    map.put("Last Name", jObject.getString("L_Name"));

                    mylist.add(map);
                }

            } catch (ClientProtocolException e) {

                e.printStackTrace();

            } 
            catch (IOException e) {

                e.printStackTrace();

            }
            // Log.i(getClass().getSimpleName(), "send  task - end");

        } catch (Throwable t) {
            responseBody = "Request failed: ";

        }

        return responseBody;
    }

    @Override
    protected void onPostExecute(String result) {

        Toast.makeText( Main.this, result,
                Toast.LENGTH_LONG).show();
        super.onPostExecute(result);
    }

}

答案 1 :(得分:0)

  

嗨,在nameValuePairs方法中都有两个字符串值,所以你必须将整数转换为字符串值。

Integer.toString(your_int_value);

使用此代码将整数转换为字符串