1

*こんにちは、C# を使用して .net サーバーにファイルをアップロードしようとしました。サーバーの場所として c:/inetpub/wwwroot を使用しているだけです。

私のJavaコードは、

 public class MainActivity extends Activity {
    InputStream inputStream;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.two);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.PNG,90, stream);
        byte[] byte_arr = stream.toByteArray();
        String image_str = Base64.encodeBytes(byte_arr);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("image",image_str));

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://127.0.0.1/AndroidUpload/uploadImage.cs");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);
            String the_response_string = convertResponseToString(response);


            Toast.makeText(this, "Response"+the_response_string, Toast.LENGTH_SHORT).show();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this,"Error1", Toast.LENGTH_SHORT).show();
        } catch (ClientProtocolException e) {
             //TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this,"Error2", Toast.LENGTH_SHORT).show();}
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this,"Error3", Toast.LENGTH_SHORT).show();
        }

    }
    public String convertResponseToString(HttpResponse response)throws IllegalStateException,IOException {
        // TODO Auto-generated method stub

        String res = "";
        StringBuffer buffer = new StringBuffer();
        inputStream = response.getEntity().getContent();
        int contentLength = (int) response.getEntity().getContentLength();
        Toast.makeText(this, "ContentLength"+contentLength, Toast.LENGTH_SHORT).show();

        if(contentLength<0){
                    }
        else{
            byte[] data = new byte[512];
            int len = 0;

            try {
                while(-1 != (len=inputStream.read(data))){
                    buffer.append(new String(data,0,len));
                }

                inputStream.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            res = buffer.toString();

            Toast.makeText(this, "Result"+res, Toast.LENGTH_SHORT).show();
        }

        return res;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

アップロード用の私のC#コードは、

 protected void Page_Init(object sender, EventArgs e)
{
  string vTitle = "";
  string vDesc = "";
  string FilePath = Server.MapPath("/files/two.png");        
  if (!string.IsNullOrEmpty(Request.Form["title"]))
  {
    vTitle = Request.Form["title"];
  }
  if (!string.IsNullOrEmpty(Request.Form["description"]))
  {
    vDesc = Request.Form["description"];
  }

  HttpFileCollection MyFileCollection = Request.Files;
  if (MyFileCollection.Count > 0)
  {
    // Save the File
    MyFileCollection[0].SaveAs(FilePath);
  }
}

Run すると、IOException がスローされます。そして、ファイルはそのフォルダーにアップロードされません。同じ結果でphpコードも試しました。問題はどこだ?

4

2 に答える 2

1

これは一般的な問題です。実際には、asp.net Web アプリを (コンピューターの) iis Web サーバーでホストし、コンピューターの IP アドレスを取得して (cmd の ipconfig コマンドで)、代わりに IP アドレスを入力する必要があり"http://127.0.0.1/AndroidUpload/..."ます。それは次のようになる可能性があります"http://192.168.1.2/AndroidUpload/..."

Android エミュレーターが 127.0.0.1 (ローカルアドレス) を理解できない

編集:

asp.net Webフォームでファイルをアップロードする方法はわかりませんが、もし私があなただったら、単純なasp.net mvcアプリケーションを作成し、コントローラーで次のようなアクションを宣言します:

[HttpPost]
        public ActionResult UploadItem()
        {
            var httpPostedFileBase = Request.Files[0];
            if (httpPostedFileBase != null && httpPostedFileBase.ContentLength != 0)
            {
                //save file in server and return a string 
        return Content("Ok");
            }
            else
            {
               return Content("Failed")
            }
    }
于 2013-07-12T07:05:50.050 に答える
1

エミュレータから PC Web サーバーに接続するには、現在 Android で使用されているアドレス指定規則に従う必要があります。より具体的に10.0.2.2は、使用したいアドレスです。

AndroidManifest.xmlまた、インターネット使用の許可が含まれていることを確認してください。乾杯

于 2013-07-12T07:15:18.143 に答える