Java 从android模拟器向服务器发送请求 我正在尝试将单个映像发布到服务器 有一个图像视图和一个按钮 我试图在点击按钮后将图像发布到服务器
我试过什么Java 从android模拟器向服务器发送请求 我正在尝试将单个映像发布到服务器 有一个图像视图和一个按钮 我试图在点击按钮后将图像发布到服务器,java,android,Java,Android,我试过什么 MainActivity.java public class MainActivity extends Activity { Button submit; ProgressDialog pDialog; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan
MainActivity.java
public class MainActivity extends Activity {
Button submit;
ProgressDialog pDialog;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);
imageView = (ImageView) findViewById(R.id.imageView1);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();
}
});
}
/**
* Method to post the image to the server.
* U will have to change the url which will accept the image data.
* @throws IOException
*/
public void postImageData() throws IOException
{
//Some random id. u can change this based on requirements
String newurl = "?" + "key=" + new Random().nextLong();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://54.218.73.244:7002/Details/"+newurl);
//Convert the bitmap drawble to a bitmap and get the string after that to send to the server
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
String dir = Environment.getExternalStorageDirectory().toString();
OutputStream fos = null;
File file = new File(dir,"temp.JPEG");
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File mFile = new File(dir, "temp.JPEG");
FileBody encFile = new FileBody(mFile,"image/jpeg");
entity.addPart("imageData", encFile);
//Another key/value parameter
//entity.addPart("UserId", new StringBody(userId));
httppost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());
System.out.println("response data:"+data);
}
public class MainTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
postImageData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
公共类MainActivity扩展活动{
按钮提交;
ProgressDialog;
图像视图图像视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit=(按钮)findViewById(R.id.submit\u按钮\u id);
imageView=(imageView)findViewById(R.id.imageView1);
submit.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
新建MainTest().execute();
}
});
}
/**
*方法将映像发布到服务器。
*您必须更改接受图像数据的url。
*@抛出异常
*/
public void postImageData()引发IOException
{
//一些随机id。u可以根据需要更改此id
字符串newurl=“?”+“key=“+new Random().nextLong();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://54.218.73.244:7002/Details/“+新网址);
//将位图drawble转换为位图,然后获取要发送到服务器的字符串
位图位图=((BitmapDrawable)imageView.getDrawable()).getBitmap();
String dir=Environment.getExternalStorageDirectory().toString();
OutputStream fos=null;
文件=新文件(dir,“temp.JPEG”);
fos=新文件输出流(文件);
BufferedOutputStream bos=新的BufferedOutputStream(fos);
bitmap.compress(bitmap.CompressFormat.JPEG,100,bos);
bos.flush();
bos.close();
MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
File mFile=新文件(dir,“temp.JPEG”);
FileBody encFile=新的文件体(mFile,“image/jpeg”);
entity.addPart(“imageData”,encFile);
//另一个键/值参数
//entity.addPart(“UserId”,新的StringBody(UserId));
httppost.setEntity(实体);
HttpClient=new DefaultHttpClient();
HttpResponse response=httpclient.execute(httppost);
字符串数据=EntityUtils.toString(response.getEntity());
System.out.println(“响应数据:”+数据);
}
公共类MainTest扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(“加载…”);
pDialog.setUndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
postImageData();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
//data=jobj.toString();
pDialog.disclose();
}
}
}
活动\u main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="135dp"
android:layout_height="181dp"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:clickable="false"
android:src="@drawable/image" />
<Button
android:id="@+id/SUBMIT_BUTTON_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="47dp"
android:text="SUBMIT" />
</LinearLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.datapostingproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.datapostingproject.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意:我还添加了所需的mime jar文件
我还检查了服务器是否正常工作 我用chrome的邮递员来测试这个 完成此测试后。。。。我可以在服务器中看到图像。。。。通过此操作,我验证服务器是否正在为此路由运行
问题出在哪里:
- 当我运行此程序并从向服务器发出post请求时 android(我正在使用android emulator发出请求)
- 我不能发布这些图片
- 我需要使这个任务从模拟器工作
来自经验丰富的专业人士的任何意见
你能在这里发布日志吗。通过emulatorpost将图像发布到服务器不会有问题,我们的服务器端代码也是@Anil Bhatiya…………我已经在服务器代码上使用了ExpressJS……我应该发布吗。。。。因为。。。。我已经清楚地表明,服务器在POSTMAN@connectinglife和Android上的功能。。。。。。。。我没有收到错误,但图像未被发布ID服务器接收请求?您在logcat中有任何错误吗?您是否成功加载图像?尝试拆分代码并分别测试每一位
final String METHOD_NAME = "ServiceName"; // our
final String SOAP_ACTION = "http://tempuri.org/ServiceName";
String result = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("IMEI", Imei);
request.addProperty("asscd", asscd);
SoapPrimitive response = null;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapPrimitive) envelope.getResponse();
//here SoapPrimitive is an important part
result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;