qq_36818440
qq_36818440
2017-06-05 04:34
采纳率: 40%
浏览 1.7k

将Android前端的数据插入后台服务器接口开发 Java代码怎么写 求解 急

Android前端数据插入后台服务器接口开发 Java代码怎么写 求解 急

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • guwei4037
    guwei4037 2017-06-05 04:37
    点赞 评论
  • chenhande1990chenhan
    -无-怨-无-悔- 2017-06-05 06:57

    package com.chenhan.net;

    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    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.util.EntityUtils;
    import org.json.JSONObject;

    public class NetworkService {

    public static int REQUEST_TIMEOUT = 30 * 1000;
    
    
    public static String getPostResult1(String url, JSONObject jsonObject){
        String url1 = "http://weather.51wnl.com/weatherinfo/GetMoreWeather?";
        url = url1 + url;
        System.out.println("url = " + url);
        UrlEncodedFormEntity entity = null;
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        NameValuePair pair1 = new BasicNameValuePair("json", jsonObject.toString());
    
        paramList.add(pair1);
        try {
            entity = new UrlEncodedFormEntity(paramList,"utf-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }   
    
        //创建http请求对象
        HttpPost post = new HttpPost(url);
        BasicHttpParams httpParams = new BasicHttpParams();
    
        HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, REQUEST_TIMEOUT);
        post.setEntity(entity);
        //创建网络访问处理对象
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        try{
            //执行请求参数�?
            HttpResponse response = httpClient.execute(post);
            //判断是否请求成功
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //获得响应信息
                String content = EntityUtils.toString(response.getEntity(),"UTF-8");
                return URLDecoder.decode(content,"utf-8");
            } else {
                //网连接失败,使用Toast显示提示信息
    
            }
    
        }catch(Exception e) {
            e.printStackTrace();
            return "{\"status\":405,\"resultMsg\":\"网络超时!\"}";
        } finally {
            //释放网络连接资源
            httpClient.getConnectionManager().shutdown();
        }
        return "{\"status\":405,\"resultMsg\":\"网络超时!\"}";
    
    }
    

    }

    点赞 评论
  • chenhande1990chenhan
    -无-怨-无-悔- 2017-06-05 07:01

    这个是Android端的 Java端的你应该会 Android端写好url和链接

    点赞 评论

相关推荐