首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

安卓app想向php段交付表单,POST方式可以吗

2013-07-08 
安卓app想向php段提交表单,POST方式可以吗?安卓app想向php段提交表单,POST方式可以吗?有什么难点,在这里。

安卓app想向php段提交表单,POST方式可以吗?
安卓app想向php段提交表单,POST方式可以吗?安卓app想向php段交付表单,POST方式可以吗
有什么难点,在这里。没做过,请指教。。
[解决办法]


/**
 * Post 方式
 * @param url网址
 * @param paramsArray参数
 * @param values值
 * @param charset编码,例如:UTF-8、GBK 
 * @return
 */
private static String postUrl(String url, String[] paramsArray, String[] values, String charset)
{
String returnConnection = null;
//封装数据
    Map<String, String> parmas = new HashMap<String, String>();
    int paramsArrayLength = paramsArray.length;
    int valuesLength = values.length;
    if(paramsArrayLength == valuesLength)
    {
    for(int i=0; i<paramsArrayLength; i++)
    {
    parmas.put(paramsArray[i], values[i]);
    }
    
    DefaultHttpClient client = new DefaultHttpClient();//http客户端
    HttpPost httpPost = new HttpPost(url);
    
    ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
    if(parmas != null)
    {
    Set<String> keys = parmas.keySet();
        for(Iterator<String> i = keys.iterator(); i.hasNext();)
        {
        String key = (String) i.next();        
            pairs.add(new BasicNameValuePair(key, parmas.get(key)));
            //System.out.println("key = " + key + ", value = " + parmas.get(key));
        }
        try
    {
    UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, charset);
    // 将POST数据放入HTTP请求
    httpPost.setEntity(p_entity);
    // 发出实际的HTTP POST请求
    HttpResponse response = client.execute(httpPost);
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK)
    {
    System.out.println("错误代码:  " + statusCode);
    }
    else
    {


    HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        returnConnection = convertStreamToString(content);
        System.out.println("结果2:" + returnConnection);
    }
    }
    catch (IllegalStateException e)
    {
    e.printStackTrace();
    System.out.println("出错1:" + e.getStackTrace());
    }
    catch (IOException e)
    {
    e.printStackTrace();
    System.out.println("出错2:" + e.getStackTrace());
    }
    }
    }
    else
    {
    System.out.println("出错3:参数不对");
    }
    return returnConnection;
}

热点排行