1

I working with this code and added my full code.I cant find what the error i did..I want to post my datas and image in server.

public class MainActivity extends Activity {
String name, price;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
String profilepic;
ProgressDialog pDialog;
private static int RESULT_LOAD_IMAGE = 1;
FileBody cbFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EditText name1 = (EditText) findViewById(R.id.editText1);
    EditText pwd = (EditText) findViewById(R.id.editText2);
    Button img = (Button) findViewById(R.id.button1);
    Button signup = (Button) findViewById(R.id.button2);
    name = name1.getText().toString();
    price = pwd.getText().toString();
    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });
    signup.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            new PostPicture().execute();

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            Toast.makeText(getApplicationContext(), "" + selectedImagePath,
                    1000).show();
            // System.out.println("Image Path : " + selectedImagePath);

        }
    }

}

private String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class PostPicture extends AsyncTask<String, String, String> {

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

    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "http://192.168.1.50/android_connect/create_product.php");

        try {

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            File file = new File(
                    "/mnt/sdcard/stock-vector-a-golden-soccer-ball-122478970.jpg");
            cbFile = new FileBody(file, "image/jpeg");
            StringBody sb1 = new StringBody("some text goes here");
            StringBody sb2 = new StringBody("some text goes here too");
            Log.d("sending picture", "guest name is " + name);
            Log.d("Sending picture", "guest code is " + price);
            // entity.addPart("name",tjh);
            entity.addPart("name", sb1);
            entity.addPart("price", sb2);
            // entity.addPart("description", cbFile);
            post.setEntity(entity);

            HttpResponse response1 = client.execute(post);
            HttpEntity resEntity = response1.getEntity();
            String Response = EntityUtils.toString(resEntity);
            Log.d("Response", Response);

        } catch (IOException e) {
            Log.e("asdf", e.getMessage(), e);

        }
        return null;

    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG)
                    .show();
        }

    }

}

}

And my php file

$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];

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

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

// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

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

    // 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);

} ?>

my logcat error 10-13 20:13:12.373: W/IInputConnectionWrapper(26253): showStatusIcon on inactive InputConnection 10-13 20:13:28.292: D/dalvikvm(26253): GC_CONCURRENT freed 215K, 5% free 7050K/7367K, paused 302ms+30ms 10-13 20:13:33.713: D/sending picture(26253): guest name is 10-13 20:13:33.713: D/Sending picture(26253): guest code is 10-13 20:13:34.976: D/Response(26253): {"success":0,"message":"Required field(s) is missing"}

karthik
  • 123
  • 13
  • Your PHP script is expecting fields called `name`, `price` and `description`, but you don't appear to be sending the `description` field. –  Oct 13 '13 at 14:51
  • @MikeW now i add description..but i get the same output – karthik Oct 13 '13 at 14:56

1 Answers1

-1

you are getting this error because your 'if' function which checks the post functions in php file is returning false, you should check your java code because some field is missing maybe description...

maxshuty
  • 9,708
  • 13
  • 64
  • 77