|
|
|
ขอ ความช่วยเหลือ เรือง android upload file,image to server |
|
|
|
|
|
|
|
ผมอยากทราบว่าาาา ผมเขียน code ใน่ส่วน upload file to server chmod 777 แล้วเรียบร้อยยย แต่ทำไม code ก้ไม่มีอะไร error แต่ เวลา upload file ขึ้น server Unknow Status! ช่วยดูให้ผมทีครับว่า code ผมผิดตรงไหน ?
Code (Android-Java)
package com.example.upload;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final int DIALOG_UPLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// txtSDCard
final EditText txtSDCard = (EditText)findViewById(R.id.txtSDCard);
// btnUpload
Button btnUpload = (Button)findViewById(R.id.btnUpload);
btnUpload.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated stub
final String strSDPath = txtSDCard.getText().toString();
final String strUrlServer = "http://202.28.77.216:80/home/android/public_html/uploadFile.php";
new UploadFileAsync().execute(strSDPath,strUrlServer);
}
});
}
public void showSuscess(String resServer)
{
/** Get result from Server (Return the JSON Code)
* StatusID = ? [0=Failed,1=Complete]
* Error = ? [On case error return custom error message]
*
* Eg Upload Failed = {"StatusID":"0","Error":"Cannot Upload file!"}
* Eg Upload Complete = {"StatusID":"1","Error":""}
*/
/*** Default Value ***/
String strStatusID = "0";
String strError = "Unknow Status!";
try {
JSONObject c = new JSONObject(resServer);
strStatusID = c.getString("StatusID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Status
if(strStatusID.equals("0"))
{
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("Error!");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setMessage(strError);
ad.setPositiveButton("Close", null);
ad.show();
}
else
{
Toast.makeText(MainActivity.this, "Upload file Successfully", Toast.LENGTH_SHORT).show();
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_UPLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Uploading file.....");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
public class UploadFileAsync extends AsyncTask<String, Void, Void> {
String resServer;
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_UPLOAD_PROGRESS);
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
int resCode = 0;
String resMessage = "";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String strSDPath = params[0];
String strUrlServer = params[1];
try {
/** Check file on SD Card ***/
File file = new File(strSDPath);
if(!file.exists())
{
resServer = "{\"StatusID\":\"0\",\"Error\":\"Please check path on SD Card\"}";
return null;
}
FileInputStream fileInputStream = new FileInputStream(new File(strSDPath));
URL url = new URL(strUrlServer);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream outputStream = new DataOutputStream(conn
.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream
.writeBytes("Content-Disposition: form-data; name=\"filUpload\";filename=\""
+ strSDPath + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
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);
// Response Code and Message
resCode = conn.getResponseCode();
if(resCode == HttpURLConnection.HTTP_OK)
{
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = 0;
while ((read = is.read()) != -1) {
bos.write(read);
}
byte[] result = bos.toByteArray();
bos.close();
resMessage = new String(result);
}
Log.d("resCode=",Integer.toString(resCode));
Log.d("resMessage=",resMessage.toString());
fileInputStream.close();
outputStream.flush();
outputStream.close();
resServer = resMessage.toString();
} catch (Exception ex) {
// Exception handling
return null;
}
return null;
}
protected void onPostExecute(Void unused) {
showSuscess(resServer);
dismissDialog(DIALOG_UPLOAD_PROGRESS);
removeDialog(DIALOG_UPLOAD_PROGRESS);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ขอบคุณร่วงหน้าครับผม
Tag : Mobile, Android
|
|
|
|
|
|
Date :
2013-09-07 22:40:05 |
By :
mr keng |
View :
1776 |
Reply :
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
งง กับ code เล็ก น้อย อ่ะครับ ว่าทำไม มันขึน Unknow Status!
|
|
|
|
|
Date :
2013-09-07 22:47:42 |
By :
keng |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พี่ วิน ตอนนี้ server ผมใช้ได้ล้วแต่ ลอง upload ทำไม มันขึ้น
มันเป็นเพราะ อะไรหรอครับพี่ วิน ผมสงสัย อะไรบ้าง ว่าทำไม upload แล้วมันขึ้น
Unfortunately,upload has stopped.
|
|
|
|
|
Date :
2013-09-08 15:00:41 |
By :
keng |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันต้อง สร้างตาราง database รึป่าว ครับพี่วิน ในการ upload
|
|
|
|
|
Date :
2013-09-08 15:57:43 |
By :
keng |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ว่าแต่ get กะ set ต้องแก้อะไรไหมครับ
|
|
|
|
|
Date :
2013-09-08 19:15:54 |
By :
มือใหม่ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2013-09-08 21:55:34 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2013-09-09 00:03:02 |
By :
keng |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดูเหมือนว่าจะมีปัญหาตอนที่ JSON ถูกส่งกลับครับ
|
|
|
|
|
Date :
2013-09-09 06:14:42 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|