我正在尝试使用android应用发布添加,但无法执行此操作,但服务器未响应。服务器错误正在使用凌空库 我的应用程序已连接到互联网,并且我有一些不同的课程
公共类User_AddPost扩展了AppCompatActivity {
Spinner ap_cat;
EditText ap_name;
EditText ap_desc;
EditText ap_rent;
ImageView ap_img;
Button ap_post;
String cat;
public static String image_url="";
private static int RESULT_LOAD_IMAGE = 1;
公共字符串urii;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user__add_post);
ap_cat=(Spinner)findViewById(R.id.ap_cat);
ap_name=(EditText)findViewById(R.id.ap_name);
ap_desc=(EditText)findViewById(R.id.ap_desc);
ap_rent=(EditText)findViewById(R.id.ap_rent);
ap_img=(ImageView)findViewById(R.id.ap_img);
ap_post=(Button)findViewById(R.id.ap_post);
ap_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
ap_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new UploadFileAsync().execute("");
}
});
ap_cat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0)cat="Electronics";
else if(i==1)cat="Cameras";
else if(i==2)cat="Cars";
else if(i==3)cat="Bikes";
else if(i==4)cat="Books";
else if(i==5)cat="Mobiles";
}
@Override
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data!=null)
{
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);
urii=picturePath;
String[]a=picturePath.split("/");
int ll=a.length-1;
image_url= "http://conjunctive-appropr.000webhostapp.com/uploads"+a[ll];
cursor.close();
ap_img.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
public void requestJsonObject()
{
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://conjunctive-appropr.000webhostapp.com/uploads.php"+"&p1="+cat+"&p2="+ap_name.getText()+"&p3="+ap_desc.getText()+"&p4="+image_url+"&p5="+ap_rent.getText();
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response)
{
/* String temp="";
int l=response.length();
for(int i=0;i<response.length()-2;i++)
{
temp+=response.charAt(i);
}*/
id=0;
if(id>0)
{
Toast.makeText(getApplicationContext(),"Add post Successfull",Toast.LENGTH_SHORT).show();
startActivity(new Intent(User_AddPost.this,Agent_Menu.class));
}
else
{
Toast.makeText(getApplicationContext(),"Something Wrong",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_SHORT).show();
// Log.d(TAG, "Error " + error.getMessage());
requestJsonObject();
}
});
queue.add(stringRequest);
}
class UploadFileAsync extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
String sourceFileUri = urii;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (sourceFile.isFile()) {
try {
String upLoadServerUri = "http://conjunctive-appropr.000webhostapp.com/uploads";
// 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("bill", sourceFileUri);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
+ sourceFileUri + "\"" + 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);
}
// send multipart form data necesssary after file
// data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn
.getResponseMessage();
if (serverResponseCode == 200) {
// m
requestJsonObject();
// essageText.setText(msg);
int x=5;
//Toast.makeText(, "File Upload Complete.",
// Toast.LENGTH_SHORT).show();
// recursiveDelete(mDirectory1);
}
// close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (Exception e) {
// dialog.dismiss();
e.printStackTrace();
int x;
}
// dialog.dismiss();
} // End else block
} catch (Exception ex) {
// dialog.dismiss();
ex.printStackTrace();
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute()
{
}
@Override
protected void onProgressUpdate(Void... values)
{
}
}
}
我希望添加成功上传
答案 0 :(得分:0)
如果您的错误日志为:E/libdataflow_monitor: open error=Operation not permitted
检查您的服务器IP地址是否错误或服务器没有响应。