2 qq 32256907 qq_32256907 于 2016.03.14 23:43 提问

求新闻app通过tomcat和mysql获取网络新闻的过程 2C

新闻app已写好,各种接口工具都有,只需要大神的详细过程怎么连接

6个回答

devmiao
devmiao   Ds   Rxr 2016.03.14 23:45
lxk_1993
lxk_1993   Rxr 2016.03.15 08:37
lly4321lly
lly4321lly   2016.03.15 08:48

用php或者javaweb做API的提供者啊

Royal_lr
Royal_lr   Ds   Rxr 2016.03.15 09:26

网络新闻是从网络上获取信息吗?

qq_32256907
qq_32256907
接近 2 年之前 回复
qq_32256907
qq_32256907
接近 2 年之前 回复
crazy1235
crazy1235   Ds   Rxr 2016.03.15 09:54

通过java服务端做接口啊。
然后app端调用接口接受返回的数据处理就行了。

u014427391
u014427391   2016.03.15 10:52

通过HTTP协议,这是代码参考,是以同步方式进行网络访问的,返回验证码为200就是请求成功!
然后通过JSON进行数据传送!希望可以帮助你!
/**
*
*以同步方式发送Http请求
*/
public class SyncHttp
{

/**
 * 通过GET方式发送请求
 * @param url URL地址
 * @param params 参数
 * @return 
 * @throws Exception
 */
public String httpGet(String url, String params) throws Exception
{
    String response = null; //返回信息
    //拼接请求URL
    if (null!=params&&!params.equals(""))
    {
        url += "?" + params;
    }

    int timeoutConnection = 3000;  
    int timeoutSocket = 5000;  
    HttpParams httpParameters = new BasicHttpParams();// Set the timeout in milliseconds until a connection is established.  
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);// Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data.  
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);  

    // 构造HttpClient的实例
    HttpClient httpClient = new DefaultHttpClient(httpParameters);  
    // 创建GET方法的实例
    HttpGet httpGet = new HttpGet(url);
    try
    {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) //SC_OK = 200
        {
            // 获得返回结果
            response = EntityUtils.toString(httpResponse.getEntity());
        }
        else
        {
            response = "返回码:"+statusCode;
        }
    } catch (Exception e)
    {
        throw new Exception(e);
    } 
    return response;
}

/**
 * 通过POST方式发送请求
 * @param url URL地址
 * @param params 参数
 * @return
 * @throws Exception
 */
public String httpPost(String url, List<Parameter> params) throws Exception
{
    String response = null;
    int timeoutConnection = 3000;  
    int timeoutSocket = 5000;  
    HttpParams httpParameters = new BasicHttpParams();// Set the timeout in milliseconds until a connection is established.  
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);// Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data.  
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);  
    // 构造HttpClient的实例
    HttpClient httpClient = new DefaultHttpClient(httpParameters);  
    HttpPost httpPost = new HttpPost(url);
    if (params.size()>=0)
    {
        //设置httpPost请求参数
        httpPost.setEntity(new UrlEncodedFormEntity(buildNameValuePair(params),HTTP.UTF_8));
    }
    //使用execute方法发送HTTP Post请求,并返回HttpResponse对象
    HttpResponse httpResponse = httpClient.execute(httpPost);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if(statusCode==HttpStatus.SC_OK)
    {
        //获得返回结果
        response = EntityUtils.toString(httpResponse.getEntity());
    }
    else
    {
        response = "返回码:"+statusCode;
    }
    return response;
}

/**
 * 把Parameter类型集合转换成NameValuePair类型集合
 * @param params 参数集合
 * @return
 */
private List<BasicNameValuePair> buildNameValuePair(List<Parameter> params)
{
    List<BasicNameValuePair> result = new ArrayList<BasicNameValuePair>();
    for (Parameter param : params)
    {
        BasicNameValuePair pair = new BasicNameValuePair(param.getName(), param.getValue());
        result.add(pair);
    }
    return result;
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!