如何在android中使用httppost方法发送字节

时间:2014-08-14 05:44:29

标签: php android arrays web-services byte

我必须使用Http post方法发送字节数组,但是在它给出的basicnamevalue对类中 我的错误,因为构造函数BasicNameValuePair(String,byte [])是undefined.is任何另一种方法来解决这个问题,请帮助我。

AsyncTask:

public void SaveDatandImage() {

     Byte[] image1,image2;

    new AsyncTask<Void, Void, String>() {

        protected void onPreExecute() {
            pDialog = new ProgressDialog(DetailsAcceptActivity.this);
            pDialog.setTitle("Sending Query");
            pDialog.setMessage("Please Wait...");
            pDialog.setCancelable(true);
            pDialog.show();
        };

        protected String doInBackground(Void... params) {

            String response = null;
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();                

            Date today = new Date();  

            nameValuePairs.add(new BasicNameValuePair("name","Name"));
            nameValuePairs.add(new BasicNameValuePair("image1",image1));
            nameValuePairs.add(new BasicNameValuePair("image2",image2));

            if (Common.isInternetConnected(DetailsAcceptActivity.this)) {
                try {
                    response = Common.httpPost(url_make_query, nameValuePairs, new String[] {});
                    // Jobj=jparser.makeHttpRequest(url_make_query, "POST", nameValuePairs);
                    Log.v(Common.TAG, "Record respose : " + response);

                    Intent intent = new Intent(DetailsAcceptActivity.this, LoginActivity.class);
                    DetailsAcceptActivity.this.finish();
                    startActivity(intent);
                    //Toast.makeText(MenuActivity.this,"Record Saved",Toast.LENGTH_SHORT).show();

                    /*JSONObject jObj = new JSONObject(response);
                    strRespCode = jObj.getString("success");
                    strRespMessage=jObj.getString("message");               

                    int success=Jobj.getInt(TAG_SUCCESS);
                    if(success==1) {
                        Toast.makeText(MenuActivity.this,"inserted", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MenuActivity.this,"Error", Toast.LENGTH_SHORT).show();
                    }*/
                    //if(response.e)
                }  catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                //groupMember.setSynced_with_server(StaticMembers.ZERO);                    
                return "NO_NETWORK";
            }
            return response;
        };

        protected void onPostExecute(String result) {
            if (result != null) {
                if(!result.equals("NO_NETWORK")) {
                    //groupMember.setSynced_with_server(StaticMembers.ONE);                         
                }   
                //Log.w("strRespMessage= "+strRespMessage, "********");
                // Toast.makeText(MenuActivity.this, "Query Sent!!", Toast.LENGTH_LONG).show();
                //finish();
            }
            pDialog.dismiss();
        }
    }.execute(null, null);
}

PHP脚本以接受来自前端的数据

<?php


// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['sender_mobile_no'])) {

  $name = $_POST['name'];
  $image1= $_POST['image1'];
  $image2= $_POST['image2'];


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);

// mysql inserting a new row
$result = mysqli_query($con,"INSERT INTO tbl_query_master(name,image1,image2) VALUES('$name','$sender_name', '$image1', '$image2')");

  // check if row inserted or not
  if ($result) {
      // successfully inserted into database
      $response["success"] = 1;
      $response["message"] = "Order placed successfully.";

      // echoing JSON response
      echo json_encode($response);
  } else  {
      // failed to insert row
      $response["success"] = 0;
      $response["message"] = "Oops! An error occurred.";

      // echoing JSON response
      echo json_encode($response);
  }
  } else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

   // echoing JSON response
   echo json_encode($response);
  }
?>

1 个答案:

答案 0 :(得分:1)

您可以将图像字节编码为String,然后将其发送到服务器上。你可以使用

String strImage=Base64.encodeToString(image1, Base64.DEFAULT); // image1 is your byte[]

然后在namevaluepair中将此String设置为

  nameValuePairs.add(new BasicNameValuePair("image1",strImage));

在php中:

你可以解码你的字符串以获得如下的bytearray。

$str=$_POST['image1'];
$abc=base64_decode($str);