您的位置:首页 > 移动开发 > Android开发

Android关于远程服务器请求

2014-06-24 00:00 253 查看
// 在Andorid开发过程中,要有http请求,这就需要我们先要验证手机网络状态,再想服务器端发出请求

// 在AndroidManifest.xml文件中添加获取联网状态权限
    <uses-permission
        android:name="android.permission.ACCESS_NETWORK_STATE" />

/* http请求代码 */

// 在AndroidManifest.xml文件中添加联网权限
<uses-permission android:name="android.permission.INTERNET" />
//
public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static JSONArray jArr = null;
    static String json = "";
//List<NameValuePair> 键值对,相当于一维关联数组
// 使用方法
//  List<NameValuePair> params = new ArrayList<NameValuePair>();
//  params.add(new BasicNameValuePair("phone", "手机号码"));
   public JSONObject getJSONObjectFROMURL(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }            

        } catch (UnsupportedEncodingException e) {
//printStackTrace 深层次的输出异常调用的流程
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON Array
        try {
        	jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Object Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐