我正在向服务器发送一个多部分请求,该服务器将包含一个图像,以及一些字符串.我还没有找到任何关于如何完成这项工作的指南,我发现的只是如何发布和获取和放置等等,但没有任何关于多部分.我很乐意提供任何帮助,谢谢
试图创建一个应用程序来下载SD卡上的文件,这是我的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
public void run() {
Log.i("step0","it starts here");
URLConnection urlConnection = null;
// TODO Auto-generated method stub
try {
//fetching the URL
Log.i("step 1.1","getting the url");
URL url = new URL("http://people.opera.com/howcome/2005/ala/sample.pdf");
Log.i("step 1.2","captured the url");
urlConnection = url.openConnection();
Log.i("step 1.3","captured the url");
urlConnection.connect();
Log.i("step 1","fetching the URL");
//specifying path and file name
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "filename.pdf"); …Run Code Online (Sandbox Code Playgroud) 我有一个方法连接发送数据到Web服务并获得响应如下:
public HttpResponse sendXMLToURL(String url, String xml, String httpClientInstanceName) throws IOException {
HttpResponse response = null;
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(httpClientInstanceName);
HttpPost post = new HttpPost(url);
StringEntity str = new StringEntity(xml);
str.setContentType("text/xml");
post.setEntity(str);
response = httpClient.execute(post);
if (post != null){
post.abort();
}
if (httpClient !=null){
httpClient.close();
}
return response;
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的片段的AsyncTask中,我尝试使用getEntity()读取响应:
HttpResponse response = xmlUtil.sendXMLToURL("url", dataXML, "getList");
//Check if the request was sent successfully
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Parse result to check success
responseText = EntityUtils.toString(response.getEntity());
if (!xmlParser.checkForSuccess(responseText, getActivity())){ …Run Code Online (Sandbox Code Playgroud) 我尝试使用以下内容创建帖子...
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
String length = String.valueOf(httppost.getEntity().getContentLength());
httppost.setHeader(HTTP.CONTENT_LEN, length); //If commented out it works
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行请求时,我收到以下错误...
10-11 22:05:02.940:W/System.err(4203):org.apache.http.client.ClientProtocolException
我猜这是因为内容长度错误.
我通过url发送Json Post请求到服务器:http://www.xyz.com/login
请求结构:
{"requestdata":{"password":"abc","devicetype":"phone","username":"amrit@pqr.com","locale":"in"},"requestcode":10}
Run Code Online (Sandbox Code Playgroud)
代码快照:
主要活动:
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("requestcode", "10"));
nameValuePair.add(new BasicNameValuePair("devicetype", "phone"));
nameValuePair.add(new BasicNameValuePair("locale", "in"));
nameValuePair.add(new BasicNameValuePair("username", "amrit@pqr.com"));
nameValuePair.add(new BasicNameValuePair("password", "abc"));
RestPost post = new RestPost(loginUrl, nameValuePair);
String Response = post.postData();
Log.i("Response:", Response);
Run Code Online (Sandbox Code Playgroud)
RestPost类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import …Run Code Online (Sandbox Code Playgroud) 我使用Android中的HttpPost将数据发送到服务器,服务器已成功接收.现在服务器响应如下:
<html><body>
<input type="text" id="nameL" name="nameL" value="Sam" />
<input type="text" id="level" name="level" value="Introductory" />
</body></html>
Run Code Online (Sandbox Code Playgroud)
如何name在Android客户端上使用输入属性(例如nameL和level)获取http响应的值?我能够获得字符串响应,但它包含<html>....</html>一个字符串中的所有标记(如上所示).
我试图用参数做一些http请求.目前我正在使用
private List<NameValuePair> mParams = new ArrayList<NameValuePair>();
private DefaultHttpClient mHttpClient;
private HttpPost mHttpPost;
Run Code Online (Sandbox Code Playgroud)
这些用于发出http请求,它工作正常.但问题是android studio显示所有这些3的弃用警告.我尝试使用,
HttpClientBuilder.create().build();但android工作室无法导入库HttpClientBuilder
我尝试下载并添加 此jar依赖 但仍然无法正常工作.这是我的代码
mHttpClient = new DefaultHttpClient();
mHttpPost = new HttpPost(url);
mParams.add(new BasicNameValuePair("key", value));
mHttpPost.setEntity(new UrlEncodedFormEntity(mParams));
Run Code Online (Sandbox Code Playgroud)
所有这些行都显示了已弃用的警告,这是另一种方法吗?
美好的一天!
我的应用程序使用简单的http客户端 - 服务器通信,在客户端与Volley库建立.首先,我启动一个授权请求:
public class AuthenticationRequest extends StringRequest {
private static final String TAG = AuthenticationRequest.class.getSimpleName();
private String login;
private String password;
public AuthenticationRequest(int method,
String url,
Response.Listener<String> listener,
Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
public void setLogin(String login) {
this.login = login;
}
public void setPassword(String password) {
this.password = password;
}
@Override
protected Map<String, String> getParams() {
HashMap<String, String> parameters = new HashMap<>();
parameters.put("login", login);
parameters.put("password", password);
return parameters;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) …Run Code Online (Sandbox Code Playgroud) android androidhttpclient android-volley android-cookiemanager
我正试图在HttpURLConnection课堂的帮助下打开一个连接.
我已经尝试过这段代码来处理一个URL,但在看到Logs时,我发现它无法获取重定向的URL.在示例代码中,您将看到要获取URL,我正在使用while循环,因此它记录了相同的URL.我迫切希望找到更好的解决问题的方法.我希望问题很清楚.
这是我正在使用的示例代码: -
Log.d(TAG,"URL received : --- >"+downloadURL);
URL url=new URL(downloadURL);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setInstanceFollowRedirects(true);
httpURLConnection.connect();
Log.d(TAG,httpURLConnection.getResponseMessage()+" Append with "+httpURLConnection.getResponseCode());
while(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_MOVED_PERM)
{
httpURLConnection.getInputStream();
URL url1=httpURLConnection.getURL();
Log.d(TAG,"Redirected URL = "+url1);
}
InputStream inptStream=httpURLConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
这是logcat的输出: -
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download …Run Code Online (Sandbox Code Playgroud) java android network-programming httpurlconnection androidhttpclient
我正在编写一段代码来执行多部分表单数据POST请求,在我的情况下,只是将图像上传到带有参数的服务器.这就是我现在拥有的:
我有一个触发多部分请求的按钮,在按钮OnClickListener中,我有这个代码来旋转一个新线程:
new Thread(new Runnable(){
@Override
public void run() {
String photoUri = getPhotoUri();
String url = getEndPointUrl();
try {
NewPostRequest.postFile(url, photoUri, <Other Params...>);
} catch (Exception e) {
// Exception Handling
}
}).start();
Run Code Online (Sandbox Code Playgroud)
而且NewPostRequest.postFile只是使用Apache Http Client发出请求,基本如下:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(fileUri);
FileBody fb = new FileBody(file);
builder.addPart("file", fb);
builder.addTextBody("param", otherParam);
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
Run Code Online (Sandbox Code Playgroud)
我需要每次都旋转一个新线程,因为最近的Android版本不允许程序在UI线程上发出http请求.但是,我真的反对旋转一个随机线程,让它像上面的代码一样失控.我曾尝试使用 …
我正在开发一个需要与远程服务器进行大量通信的Android应用程序.为此,我编写了一个处理所有网络通信的类.
我是否需要为每一种方法制作一个Asynctask?对于其余的代码执行,我依赖的方法呢?(因此需要同步完成 - 比如在注册时等待答案?)
我问这个是因为我之前已经有一个小应用程序与远程服务器进行通信而我没有使用任何Asynctasks,这一个在所有被调用的方法崩溃.
编辑 -
同时 - 在创建我自己的课程之前,我发现了一个与谷歌图书馆相关的精彩教程已经处理了图书馆名称是Volley我看过的教程就是这个
我试图使用android httpclient(loopj)发布一些数据.我在其正文中添加一些json数据并设置请求标头.但它显示AsyncHttpClient:将忽略Passed contentType,因为HttpEntity设置了内容类型.有谁知道如何解决这个问题?
public static void post(Activity context,String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
try {
JSONObject jsonParams = new JSONObject();
JSONObject innerObject = new JSONObject();
innerObject.put("Name", "@MODE");
innerObject.put("ParamType", "8");
innerObject.put("Value", "0");
JSONArray ar = new JSONArray();
ar.put(innerObject);
try {
jsonParams.put("ProcName", "Core.MENUS_SPR");
jsonParams.put("dbparams", ar);
Log.i("jsonParams.toString()",""+jsonParams.toString());
StringEntity se = null;
try {
se = new StringEntity(jsonParams.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return;
}
client.post(context, (url), se, "application/json", responseHandler);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) { …Run Code Online (Sandbox Code Playgroud) android web-services loopj androidhttpclient android-async-http
我使用android-async-http,并覆盖所有onSuccess和onFailure方法,但我收到错误:
onSuccess(int, Header[], JSONObject) was not overriden, but callback was received.
cz.msebera.android.httpclient.client.HttpResponseException: Not Found
Run Code Online (Sandbox Code Playgroud)
我只是想知道我真的覆盖了这个方法.我也得到了日志:onSuccess JSONObject:{"error":null,"success":false}
public static void querySecurityCode(String username) {
RequestParams params = new RequestParams();
params.put("username", username);
VStarRestClient.getClient().setEnableRedirects(true);
VStarRestClient.post(GET_SECURITY_CODE_URL, params, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Log.i("ws", "---->>onSuccess JSONObject:" + response);
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
Log.i("ws", "---->>onSuccess JSONArray");
}
@Override
public …Run Code Online (Sandbox Code Playgroud) android ×13
java ×4
httpresponse ×2
deprecated ×1
http ×1
http-post ×1
httphandler ×1
json ×1
loopj ×1
request ×1
sockets ×1
web-services ×1