Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文件从android传输到php服务器的更改_Android - Fatal编程技术网

将文件从android传输到php服务器的更改

将文件从android传输到php服务器的更改,android,Android,您好,我编写了一段代码,将json对象从android手机传输到php服务器,如下所示。如果我想将文件传输到服务器,请建议我对代码所需的更改 private String constructDataRecord(JSONObject data, String type) { JSONObject dataRecord = new JSONObject(); try { dataRecord.put("date", timeStr);

您好,我编写了一段代码,将json对象从android手机传输到php服务器,如下所示。如果我想将文件传输到服务器,请建议我对代码所需的更改

private String constructDataRecord(JSONObject data, String type)
{
    JSONObject dataRecord = new JSONObject();
    try
        {
            dataRecord.put("date", timeStr);
            dataRecord.put("time_stamp", c.getTimeInMillis());
            dataRecord.put("user", IMEI);
            dataRecord.put("type", type);
            dataRecord.put("ver", VER);
            dataRecord.put("data", data);
        }
        catch (JSONException e)
        {
            Log.e(TAG, "Exception", e);
        }

        String url = "http://mpss.csce.uark.edu/~smandava/index.php";
        Map<String, String> kvPairs = new HashMap<String, String>();
        kvPairs.put("data", dataRecord.toString());
        Log.d(TAG, "Exception");
        HttpResponse re = null;


        try {
            re = doPost(url, kvPairs);
            String  temp  = EntityUtils.toString(re.getEntity());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


      //  if (temp.compareTo("SUCCESS")==0)
        {
     //       Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show();
        }

        return dataRecord.toString();
    }


   public static HttpResponse doPost(String url, Map<String, String> kvPairs) throws ClientProtocolException, IOException 
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        if (kvPairs != null && kvPairs.isEmpty() == false) 
        {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(kvPairs.size());
            String k, v;
            Iterator<String> itKeys = kvPairs.keySet().iterator();
            while (itKeys.hasNext()) 
            {
                k = itKeys.next();
                v = kvPairs.get(k);
                nameValuePairs.add(new BasicNameValuePair(k, v));
            }             
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }
        HttpResponse response;
        response = httpclient.execute(httppost);
        return response;
    }
私有字符串constructDataRecord(JSONObject数据,字符串类型)
{
JSONObject dataRecord=新的JSONObject();
尝试
{
dataRecord.put(“日期”,timeStr);
put(“时间戳”,c.getTimeInMillis());
dataRecord.put(“用户”,IMEI);
dataRecord.put(“type”,type);
数据记录。put(“ver”,ver);
dataRecord.put(“数据”,data);
}
捕获(JSONException e)
{
Log.e(标签“例外”,e);
}
字符串url=”http://mpss.csce.uark.edu/~smandava/index.php”;
Map kvPairs=newhashmap();
kvPairs.put(“数据”,dataRecord.toString());
Log.d(标记为“例外”);
HttpResponse re=null;
试一试{
re=doPost(url,kvPairs);
字符串temp=EntityUtils.toString(re.getEntity());
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//如果(温度比较(“成功”)==0)
{
//Toast.makeText(这是“发送完成!”,Toast.LENGTH\u LONG.show();
}
返回dataRecord.toString();
}
公共静态HttpResponse doPost(字符串url,映射kvPairs)抛出ClientProtocolException,IOException
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
if(kvPairs!=null&&kvPairs.isEmpty()=false)
{
List nameValuePairs=新的ArrayList(kvparies.size());
串k,v;
迭代器itKeys=kvPairs.keySet().Iterator();
while(itKeys.hasNext())
{
k=itKeys.next();
v=kvPairs.get(k);
添加(新的BasicNameValuePair(k,v));
}             
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}
HttpResponse响应;
response=httpclient.execute(httppost);
返回响应;
}

您可以看到一个例子