PDA

View Full Version : گرفتن مقادیر برگشتی در HttpURLConnection (می خوام مثل جیسون، مقادیر رو برگردونه ولی نمیتونم.)



dalmif
جمعه 24 بهمن 1393, 18:03 عصر
سلام دوستان
من یه کلاس آپلود فایل دارم به شکل زیر




public static int uploadFile(String pp , Activity fa) {


//String pp2 = pp;
uploadFileName = pp;
//Activity context1 = fa ;
final Activity context = fa;
final String fileName = pp;
final String IP = fa.getString(R.string.IP);
upLoadServerUri= IP + "UploadToServer.php";
final File sourceFile = new File(fileName);



if (!sourceFile.isFile()) {


Log.e("uploadFile", "Source File not exist :"
+uploadFileName );


context.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();

}
});

return 0;

}
else
{

Thread dd1 = new Thread() {

@Override
public void run() {

// TODO Auto-generated method stub
super.run();



try {








// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("enctype", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);

//conn.setRequestProperty("Password", Password);





// conn.setRequestProperty("Content-Type", "text/json; charset=utf-8");

Log.i("uploadFile", "salam " + fileName + " " + "conn.getOutputStream()" );


dos = new DataOutputStream(conn.getOutputStream());
// dos = new DataOutputStream (conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);

// create a buffer of maximum size
bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {

dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

}
Log.i("uploadFile", "salam " + fileName + " " + "conn.getOutputStream()" );

// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();

Log.i("uploadFile", "HTTP Response is : "
+ serverResponseMessage + ": " + serverResponseCode);

if(serverResponseCode == 200){


context.runOnUiThread(new Runnable() {
public void run() {



try {
OutputStream ff = conn.getOutputStream();
Log.i("fArSI", "dd: " +ff);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


























new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE)
.setTitleText("فایل شما آپلود شد")
.setContentText("بر روی گزینه زیر کلیک کنید")
.show();
inti = 200;

}
});
}

//close the streams //
fileInputStream.close();
dos.flush();
dos.close();












}



catch (MalformedURLException ex) {

ex.printStackTrace();

context.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, "MalformedURLException",
Toast.LENGTH_SHORT).show();
}
});

Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
}






catch (Exception e) {


e.printStackTrace();

context.runOnUiThread(new Runnable() {
public void run() {

Toast.makeText(context, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});
Log.e("Upload file to server Exception", "Exception : "
+ e.getMessage(), e);
}



}




};
dd1.start();

return inti;

} // End else block


}



و سالم است و کار میکنه اما من می خوام علاوه بر اینکه این عکس آپلود بشه، آدرسش به همراه نام کاربری فرد آپلود کننده هم در دیتابیس ثبت بشه اما تا اونجا که آدرسش ثبت میشه رو نوشتم (با PHP) اما هر کاری می کنم نمیتونم به همراه اون مقداری که از گوشی به سرور میره تا آپلود صورت بگیره یک اسم ارسال کنم.
دوستان لطفا بگید باید چی رو به کجای کد اضافه کنم تا بتونم نام کاربری رو هم بفرستم برای سرور.
تورو خدا کمک کنید