• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to upload JSON Data and Image to php server Sucessfully

nagamothu

Newbie
Hi am Fresher in android . am creating a app which i need to upload captured image and JSON data which i got from QRcode to PHP server. But am getting Secret_key not matched message when i debug.

I Checked API from postman the server is responding with the file path which i uploaded. but from code am getting secret key not match. please help guys.

this is my code

Code:
private class uploadFileToServerTask extends AsyncTask <String, String, Object> {
private DataOutputStream outputStream;;
[USER=1021285]@override[/USER]
protected String doInBackground(String... args) {
try {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
@SuppressWarnings("PointlessArithmeticExpression")
int maxBufferSize = 1 * 1024 * 1024;
String result = "";
String filename = args[0];
java.net.URL url = new URL( ApplicationConstant.BASE_URL );
Log.d( ApplicationConstant.TAG, "url " + url );
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Allow Inputs &amp; Outputs.
connection.setDoInput( true );
connection.setDoOutput( true );
connection.setUseCaches( false );
// Set HTTP method to POST.
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Connection", "Keep-Alive" );
connection.setRequestProperty( "Content-Type", "multipart/form-data;boundary=" + boundary );
connection.setRequestProperty( "secret_key", ApplicationConstant.secret_key);
outputStream= new DataOutputStream( connection.getOutputStream() );
FileInputStream fileInputStream;
{
outputStream.writeBytes( twoHyphens + boundary + lineEnd );
outputStream.writeBytes( "Content-Disposition: form-data; name=\"file\";filename=\"" + filename + "\"" + lineEnd );
outputStream.writeBytes( lineEnd );
outputStream.writeBytes( twoHyphens + boundary + lineEnd );
//outputStream.writeBytes( query );
Log.d( ApplicationConstant.TAG, "filename " + filename );
fileInputStream = new FileInputStream( filename );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min( bytesAvailable, maxBufferSize );
buffer = new byte[bufferSize];
// Read file
// bytesRead = fileInputStream.read( buffer, 0, bufferSize );
outputStream.write( buffer, 0, bufferSize );
bytesAvailable = fileInputStream.available();
bufferSize = Math.min( bytesAvailable, maxBufferSize );
bytesRead = fileInputStream.read( buffer, 0, bufferSize );
outputStream.writeBytes( lineEnd );
outputStream.writeBytes( twoHyphens + boundary + twoHyphens + lineEnd );
}
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d( "serverResponseCode", "" + serverResponseCode );
Log.d( "serverResponseMessage", "" + serverResponseMessage );
//Response from server
String response = "";
if (serverResponseCode == HttpURLConnection.HTTP_OK) {
InputStream responseStream = new
BufferedInputStream( connection.getInputStream() );
BufferedReader responseStreamReader =
new BufferedReader( new InputStreamReader( responseStream ) );
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
stringBuilder.append( line ).append( "\n" );
}
responseStreamReader.close();
response = stringBuilder.toString();
connection.disconnect();
fileInputStream.close();
outputStream.flush();
outputStream.close();
if (serverResponseCode == 200) {
return "true";
}
} else {
throw new IOException( "Server returned non-OK status: " + serverResponseCode );
}
return response;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}catch(
Exception e)
{
e.printStackTrace();
}
return"false";
}
 
Last edited by a moderator:
Use [code][/code] to format code fragments. And you've lost all the indentation, which makes it hard to read.
 
Also what is BASE_URL? Are you using HTTPS? In which case, you need to share your public key with the remote server, and use the corresponding private key to encode.
 
Ok, so have you created a self-signed certificate? This will generate your public/private encryption key pair.
See here for some background information

https://developer.android.com/training/articles/security-ssl

No actually i didnot create my TL gave me the URL. i did not post the URL here publicly. in the place of URL there is actual URL . i posted the format to tell the type of URL. in postman the secret key should be added in the body and not in header.how to add secret key in body instead of header? please tell me.
 

Attachments

  • Screenshot (215).png
    Screenshot (215).png
    172.7 KB · Views: 245
So that looks like an error message produced by your server side PHP script. What does that look like?
 
So that looks like an error message produced by your server side PHP script. What does that look like?

I dont have server side script i just have URL and secret key .if the error is from server side from post man also we will get same error write . but from post man am getting sucess message. What i find out in postman is when i sent secret key in header a getting the same message which am getting in android studio(Secret_key not match). when i pass in body am getting sucess message.
how to resolve thos. i think am sending secret key in header in code instead of sending it in body.
 
So there are three possibilities:-

1. You don't have the correct "secret key"
2. You're sending the the key in the wrong property on the POST request
3. The server PHP script is doing something wrong

If 2) or 3) then you need to have a look at the PHP script to understand what it's doing.
 
I just noticed that you said it worked when you put the key in the POST request body.
So the server isn't looking for the data in the header, just the body.
Why is this a problem to use the body data instead of the header?
 
I just noticed that you said it worked when you put the key in the POST request body.
So the server isn't looking for the data in the header, just the body.
Why is this a problem to use the body data instead of the header?

thats what my doubt too, how to send the data to body programatically .
 
Hi i have a file which contains image and JSON data. i want to upload it to a URL which has params file and secret key via multipart post request . and those params i should send in body instead of header to URL(Tried in postman). If anybody have sample code please attach it here guys.
 
Threads merged into existing thread in Android Development forum.

So what does your code look like now, and what's the problem. Please post your updated code.
 
Threads merged into existing thread in Android Development forum.

So what does your code look like now, and what's the problem. Please post your updated code.


Hi i have added secret_key on output stream. but the execution stops when it comes to
outputStream.writeBytes( twoHyphens + boundary + lineEnd );

And am attaching screen shots of file and where i attached secret key please see.And i attached console also please see
 

Attachments

  • Screenshot (218).png
    Screenshot (218).png
    153.8 KB · Views: 235
  • Screenshot (217).png
    Screenshot (217).png
    156.7 KB · Views: 263
  • Screenshot (219)_LI.jpg
    Screenshot (219)_LI.jpg
    1.3 MB · Views: 256
Question to you: Do you know what a NullPointerException is, and what causes it?
 
Back
Top Bottom