相关疑难解决方法(0)

如何使用java.net.URLConnection来触发和处理HTTP请求

java.net.URLConnection在这里经常询问使用情况,Oracle教程对此非常简洁.

该教程基本上只显示了如何触发GET请求并读取响应.它没有解释如何使用它来执行POST请求,设置请求标头,读取响应标头,处理cookie,提交HTML表单,上传文件等.

那么,我如何使用java.net.URLConnection触发和处理"高级"HTTP请求?

java http urlconnection httprequest httpurlconnection

1903
推荐指数
11
解决办法
100万
查看次数

使用带HttpURLConnection的POST发送文件

由于Android开发人员建议使用HttpURLConnection该类,我想知道是否有人可以为我提供一个很好的示例,说明如何通过POST将位图"文件"(实际上是内存中的流)发送到Apache HTTP服务器.我对cookie或身份验证或任何复杂的东西不感兴趣,但我只想拥有一个可靠的逻辑实现.我在这里看到的所有例子看起来更像是"让我们试试这个,也许它有效".

现在,我有这个代码:

URL url;
HttpURLConnection urlConnection = null;
try {
    url = new URL("http://example.com/server.cgi");

    urlConnection = (HttpURLConnection) url.openConnection();

} catch (Exception e) {
    this.showDialog(getApplicationContext(), e.getMessage());
}
finally {
    if (urlConnection != null)
    {
        urlConnection.disconnect();
    }
}
Run Code Online (Sandbox Code Playgroud)

showDialog应该只显示一个AlertDialog(如果URL无效?).

现在,让我们说我生成一个这样的位图:Bitmap image = this.getBitmap()在一个派生的控件中View,我想通过POST发送它.实现这样的事情的正确程序是什么?我需要使用哪些课程?我可以HttpPost这个例子中使用吗?如果是这样,我将如何构建InputStreamEntity我的位图?我觉得要首先将位图存储在设备上的文件中是令人反感的.


我还要提一下,我真的需要将原始位图的每个未经改变的像素发送到服务器,因此我无法将其转换为JPEG.

post android client-server http httpurlconnection

119
推荐指数
4
解决办法
15万
查看次数

如何使用Java HttpClient库上传文件

我想编写Java应用程序,用PHP将文件上传到Apache服务器.Java代码使用Jakarta HttpClient库版本4.0 beta2:

import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;


public class PostFile {
  public static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost("http://localhost:9002/upload.php");
    File file = new File("c:/TRASH/zaba_1.jpg");

    FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");

    httppost.setEntity(reqEntity);
    reqEntity.setContentType("binary/octet-stream");
    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println(EntityUtils.toString(resEntity)); …
Run Code Online (Sandbox Code Playgroud)

php java java-http-client

51
推荐指数
2
解决办法
12万
查看次数

Android:使用MultiPartEntityBuilder上传Image和JSON

我尝试将数据上传到服务器,我的数据包含多个图像和大JSON,在它之前,我尝试使用转换图像发送到字符串使用base64并发送我之前转换的另一个数据和图像JSON,但我OutOfMemory在这里遇到问题,所以我读了一个说我必须尝试使用​​的解决方案MultipartEntityBuilder.我仍然感到困惑,不明白该怎么做MultiPartEntityBuilder,有没有人可以帮助我这样做MultiPartEntityBuilder?这是我的代码:

    try{
    //membuat HttpClient
    //membuat HttpPost
    HttpPost httpPost= new HttpPost(url);
    SONObject jsonObjectDP= new JSONObject();
    System.out.println("file audio "+me.getModelDokumenPendukung().getAudio());
    jsonObjectDP.put("audio_dp",MethodEncode.EncodeAudio(me.getModelDokumenPendukung().getAudio()));
    jsonObjectDP.put("judul_audio",me.getModelDokumenPendukung().getJudul_audio());
    jsonObjectDP.put("ket_audio",me.getModelDokumenPendukung().getKet_audio());
    JSONArray ArrayFoto= new JSONArray();

    //This loop For my multiple File  Images
    List<ModelFoto>ListFoto=me.getModelDokumenPendukung().getListFoto();
    for (int i=0; i<ListFoto.size();i++) {
        JSONObject jsonObject= new JSONObject();
        jsonObject.put("foto", ListFoto.get(i).getFile_foto());
        jsonObject.put("judul_foto", ListFoto.get(i).getJudul_foto());
        jsonObject.put("ket_foto", ListFoto.get(i).getKet_foto());
        ArrayFoto.put(jsonObject);

    }

    JSONObject JSONESPAJ=null;
     JSONESPAJ = new JSONObject();
     JSONObject JSONFINAL = new JSONObject();
            JSONESPAJ.put("NO_PROPOSAL",me.getModelID().getProposal());
            JSONESPAJ.put("GADGET_SPAJ_KEY",me.getModelID().getIDSPAJ());
            JSONESPAJ.put("NO_VA",me.getModelID().getVa_number());
            JSONESPAJ.put("Dokumen_Pendukung",jsonObjectDP);

            JSONFINAL.put("ESPAJ", JSONESPAJ); …
Run Code Online (Sandbox Code Playgroud)

android json http-post multipartentity

12
推荐指数
1
解决办法
3万
查看次数

使用PHP将文件从android(java)上传到服务器

您好我试图使用PHP将文件从我的Android应用程序上传到我的服务器.

我看过这篇文章:

如何使用与PHP一起使用的Java HttpClient库上传文件 http://www.veereshr.com/Java/Upload 如何使用http将Android中的文件从移动设备发送到服务器?

这是我的JAVA代码:

public void upload() throws Exception {

        File file = new File("data/data/com.tigo/databases/exercise");

        Log.i("file.getName()", file.getName());

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://***.***.***.***/backDatabase.php");

        InputStreamEntity reqEntity = new InputStreamEntity( new FileInputStream(file), -1);
        reqEntity.setContentType("binary/octet-stream");    

        reqEntity.setChunked(true);
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);

        if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){
            // Successfully Uploaded
            Log.i("uploaded", response.getStatusLine().toString());
        }
        else{
            // Did not upload. Add your logic here. Maybe you want to retry.
            Log.i(" not uploaded", response.getStatusLine().toString());
        }

        httpclient.getConnectionManager().shutdown();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的PHP代码:

<?php


    $uploads_dir = '/tigo/databaseBackup'; …
Run Code Online (Sandbox Code Playgroud)

php java android

5
推荐指数
1
解决办法
2万
查看次数