我想在其他方法中使用请求http“result”(方法获取数据)的返回来创建包含在文件名中的结果的文件名。 谢谢你的帮助
first method
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.x.x/x/id1.php");
// Depends on your web service
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON =result;
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
第二种方法
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
Config.IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed create "
+ Config.IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile
//i want to use it here like "IMG_VG"+result+...
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_VG" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_VG" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
答案 0 :(得分:0)
为什么不在AsyncTask中使用第二种方法?它不是返回下载的数据,而是返回一个从'doInBackground'方法中的数据创建的文件。然后将文件返回到'onPostExecute'中的Activity。
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, File> {
@Override
protected File doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.x.x/x/id1.php");
// Depends on your web service
InputStream inputStream = null;
File outputFile = getOutputMediaFile(0);
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
//TODO check if result is 200 OK or you'll write the error in the file.
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
FileWriter writer = new FileWriter(outputFile);
writer.write(result);
writer.close();
return outputFile;
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return outputFile;
}
@Override
protected void onPostExecute(File result){
myFile =result;
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}