【发布时间】:2014-11-06 23:49:26
【问题描述】:
我正在使用 cloudinary 上传图片。
当我尝试在onCreate 中执行此操作时,它给出了NetworkOnMainThread 的明显错误。所以现在我尝试使用AsyncTask,但现在结果是NPE。
09-12 23:36:18.443: E/AndroidRuntime(24715): Caused by: java.lang.NullPointerException
09-12 23:36:18.443: E/AndroidRuntime(24715): at com.example.dbms.UploadActivity$Upload.doInBackground(UploadActivity.java:51)
查看的代码如下:
public class UploadActivity extends Activity {
private ProgressDialog dialog = null;
Cloudinary cloudinary;
JSONObject Result;
String file_path;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
Intent i = getIntent();
file_path = i.getStringExtra("file");
HashMap config = new HashMap();
config.put("cloud_name", "dbms");
config.put("api_key", "key");//I have changed the key and secret
config.put("api_secret", "secret");
Cloudinary cloudinary = new Cloudinary(config);
TextView textView = (TextView) findViewById(R.id.text5);
textView.setText(file_path);
file = new File(file_path);
}
private class Upload extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
try {
Result = cloudinary.uploader().upload(file,
Cloudinary.emptyMap());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
TextView textView = (TextView) findViewById(R.id.text5);
textView.setText("file uploaded");
}
}
public void onClick(View view) {
Upload task = new Upload();
task.execute(new String[] { "http://www.vogella.com" });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.upload, menu);
return true;
}
}
关注的行是:
Result = cloudinary.uploader().upload(file,
Cloudinary.emptyMap());
【问题讨论】:
-
找出什么是空的(可能是
cloudinary)并确保它不是。
标签: android android-asynctask cloudinary