gpt4 book ai didi

java - Android 应用程序发布除发布文件之外的所有内容

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:07 26 4
gpt4 key购买 nike

我在我的 Logcat 中使用 apache-mime4j-0.6.jarhttpmime-4.0.1.jar 一切都很好,除了这个 Log out_write()使用标记 audio_hw_primary 将 sleep 时间限制为 31178 到 23219,但我不确定它是否来 self 的应用。

问题:在我的 php 文件中,我可以接收除我发布的文件之外的所有内容!

这是我的代码,基于this问题

package com.negano.Uploader;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


public class ActivityMain extends Activity {

private static DefaultHttpClient mHttpClient;


public static void ServerCommunication() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
mHttpClient = new DefaultHttpClient(params);
}


public void uploadUserPhoto(File image) {

try {

HttpPost httppost = new HttpPost("http://logcat.ir/uproid.php");

MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("Title", new StringBody("Title"));
multipartEntity.addPart("Nick", new StringBody("Nick"));
multipartEntity.addPart("Email", new StringBody("Email"));
multipartEntity.addPart("Image", new FileBody(image));
httppost.setEntity(multipartEntity);

HttpResponse result = mHttpClient.execute(httppost);
InputStream stream;
stream = result.getEntity().getContent();
String response = inputstreamToString(stream);
Log.i("negano", "response is " + response);
}
catch (Exception e) {
e.printStackTrace();
}
}


public static String inputstreamToString(InputStream inputStream) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();

try {
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}

return builder.toString();
}
catch (Exception e) {
e.printStackTrace();
}

return null;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String path = Environment.getExternalStorageDirectory() + "/1.jpg";
findViewById(R.id.upload).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ServerCommunication();
uploadUserPhoto(new File(path));
}
});

}
}

最佳答案

这对我有用:

package ir.negano.Downloader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Handler;


public class Downloader {
public static final Handler HANDLER = new Handler();
static Thread DownloadThread;


public static interface OnDownloadMoveListener {

public void onRun(int FileSize, int DownloadedSize, int DownloadedPersent);
}


public static void Download(final String onlineFilePath, final String localFilePath, final OnDownloadMoveListener DownloadListener) {
DownloadThread = new Thread(new Runnable() {

int DownloadedSize = 0;
int DownloadedPersent = 0;


@Override
public void run() {
try {
URL url = new URL(onlineFilePath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
final int FileSize = connection.getContentLength();

InputStream inputStream = connection.getInputStream();
FileOutputStream OutPutStream = new FileOutputStream(localFilePath);
byte[] buffer = new byte[8 * 1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
OutPutStream.write(buffer, 0, len);
DownloadedSize += len;
DownloadedPersent = (int) (((float) DownloadedSize / FileSize) * 100);
if (DownloadListener != null)
{
HANDLER.post(new Runnable() {

@Override
public void run() {
DownloadListener.onRun(FileSize, DownloadedSize, DownloadedPersent);
}
});

}
if (DownloadedPersent >= 100)
{
DownloadThread = null;
}
}
OutPutStream.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
DownloadThread.start();

}

关于java - Android 应用程序发布除发布文件之外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23566604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com