-4

我修改了人们提供的代码,我面临一个问题,它在 Android 中运行得非常好。但是..它没有给出任何响应,也没有给出数据库..函数之后行仍然保持不变..我犯了什么错误?谢谢!

Android代码在这里:

    public class MainActivity extends Activity {
        private ProgressDialog pDialog;
    Uri currImageURI;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button gallery_btn = (Button)findViewById(R.id.gallerybtn);
    gallery_btn.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            //to open up a gallery browser
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Picture.."),1);          

        }

    });

    Button upload_btn = (Button)findViewById(R.id.uploadbtn);
    upload_btn.setOnClickListener(new OnClickListener(){
        public void onClick(View view){

            new HttpUploader().execute(getRealPathFromURI(currImageURI));       

        }

    });

}   

// To handle when an image is selected from the browser
public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            // currImageURI is the global variable I’m using to hold the content:
            currImageURI = data.getData();

            EditText tv_path = (EditText) findViewById(R.id.path);
            tv_path.setText(getRealPathFromURI(currImageURI));

        }

    }
}

//Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI( Uri contentUri) {
    String [] proj={MediaStore.Images.Media.DATA};
    android.database.Cursor cursor = managedQuery(contentUri,
            proj,     // Which columns to return
            null,     // WHERE clause; which rows to return (all rows)
            null,     // WHERE clause selection arguments (none)
            null);     // Order-by clause (ascending by name)
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String c = cursor.getString(column_index); 
    return c;
}

public class HttpUploader extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Uploading Image.. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

     protected String doInBackground(String... path) {

         String outPut = null;

         for (String sdPath : path) {

             Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
             ByteArrayOutputStream bao = new ByteArrayOutputStream();

             //Resize the image
             double width = bitmapOrg.getWidth();
             double height = bitmapOrg.getHeight();
             double ratio = 400/width;
             int newheight = (int)(ratio*height);

             System.out.println("———-width" + width);
             System.out.println("———-height" + height);

             bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);

             //Here you can define .PNG as well
             bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
             byte[] ba = bao.toByteArray();
             String ba1=Base64.encodeBytes(ba);

             String id = "99";
             ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
             nameValuePairs.add(new BasicNameValuePair("image", ba1));
             nameValuePairs.add(new BasicNameValuePair("path", sdPath));
             nameValuePairs.add(new BasicNameValuePair("id", id));

             try {
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://127.0.0.1/upload.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                 HttpResponse response = httpclient.execute(httppost);
                 HttpEntity entity = response.getEntity();                

                 // print responce
                 outPut = EntityUtils.toString(entity);
                 Log.i("GET RESPONSE—-", outPut);

                 //is = entity.getContent();
                 Log.e("log_tag ******", "good connection");

                 bitmapOrg.recycle();

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

     protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
 }
}

Logcat 只显示这个:log_tag ** ", "good connection

PHP文件放在这里:

<?php
 include "db_connection.php";

if (isset($base)) {
    $base = $_REQUEST["image"];
    $filepath = $_REQUEST["path"];
    $id = $_REQUEST["id"];

    $image_name = "/image/".$filepath;

    // base64 encoded utf-8 string
    $binary = base64_decode($base);

    // binary, utf-8 bytes

    header("Content-Type: bitmap; charset=utf-8");

    $file = fopen($image_name, "wb");

    fwrite($file, $binary);

    fclose($file);

    $result = mysql_query("UPDATE restaurants SET image = '$image_name' WHERE ID = '$id'");

    if(mysql_affected_rows > 0){
        echo json_encode("success!");
    }else{
        echo json_encode("failed!");
    }

} else {
       echo json_encode("missing required attribute!"); 
}

?>        
4

1 回答 1

3

替换此代码部分:

$result = mysql_query("更新餐厅 SET image = '$image_name' WHERE ID = '$id'");

if(mysql_affected_rows() <> 0){
    echo json_encode("success!");
}else{
    echo json_encode("failed!");
}
于 2013-03-23T11:02:28.287 回答