Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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
Php 用于将数据上载到服务器的JSON代码_Php_Android_Mysql_Database_Json - Fatal编程技术网

Php 用于将数据上载到服务器的JSON代码

Php 用于将数据上载到服务器的JSON代码,php,android,mysql,database,json,Php,Android,Mysql,Database,Json,我想把我的图像数据文件和字符串数据用户名从android上传到服务器上的mysql数据库 我的JSON代码: private void uploadFile() { // TODO Auto-generated method stub String nama = getIntent().getStringExtra("user"); Bitmap bitmapOrg= BitmapFactory.decodeFile(Environment.get

我想把我的图像数据文件和字符串数据用户名从android上传到服务器上的mysql数据库

我的JSON代码:

private void uploadFile() {
        // TODO Auto-generated method stub
        String nama = getIntent().getStringExtra("user");
        Bitmap bitmapOrg= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Chart1.png");
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
        byte [] ba = bao.toByteArray();
        String ba1=Base64.encodeBytes(ba);
        ArrayList nameValuePairs = new ArrayList();
        nameValuePairs.add(new BasicNameValuePair("file",ba1));
        nameValuePairs.add(new BasicNameValuePair("username",nama));
        try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new
        HttpPost("http://ipadress/base.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse httpRespose = httpclient.execute(httppost);
       HttpEntity httpEntity = httpRespose.getEntity();
       InputStream in = httpEntity.getContent();
       BufferedReader read = new BufferedReader(new InputStreamReader(in));

       String isi= "";
       String baris= "";

       while((baris = read.readLine())!=null){
          isi+= baris;
       }

           //Jika isi tidak sama dengan "null " maka akan tampil Toast "Register Success" sebaliknya akan tampil "Register Failure"
           if(!isi.equals("null")){                  
               Toast.makeText(this, "Register Success", Toast.LENGTH_LONG).show();
           }else{
               Toast.makeText(this, "Register Failure", Toast.LENGTH_LONG).show();
           }

        }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
        }

  }
和我的php代码:

<?php
include_once("koneksi.php");

$username = $_REQUEST['username'];
$file = $_REQUEST['file'];

$hasil = mysql_query("select (max(ID)+1)as newid  from userownfile"); 
$row = mysql_fetch_row($hasil); 

$base = $_REQUEST['file'];
$filename = $row[0] . ".jpg";
$buffer=base64_decode($base);
$path = "img/".$filename.".jpg";
$handle = fopen($path, 'wb');
$numbytes = fwrite($handle, $buffer);
fclose($handle);
$conn=mysql_connect("localhost","root","");
mysql_select_db("db_bloodglucose");

if($username && $file){
$sql = "insert into userownfile(username,file) values('$username','" . 

$path . "')";
mysql_query($sql);
}

$string= "select * from userownfile";
$my_string= mysql_query($string);
if($my_string){
   while($object= mysql_fetch_assoc($my_string)){
      $output[] = $object;
   }

   echo json_encode($output);

?>
我在数据库中的表应该如下所示:

ID |用户名|文件


但当我尝试上传时,图片和用户名并没有插入到我的数据库中,我是否遗漏了什么?有人能帮我修改代码吗?感谢您使用Fiddler之类的工具确保发送到服务器的数据包就是服务器期望的数据包

如果您想将文本格式化为JSON,我建议您使用内置类并执行以下操作:

JSONObject json = new JSONObject();
json.put("file", ba1);
json.put("username", nama);