douxia3505
2014-05-27 12:36
浏览 63

Android:我如何从我的Android应用程序发送文件到openshift上的远程服务器?

what i'm trying to do is to save a file in a remote server which is created on openshift (i created an application with php 5.3). this is the code which sends a file:

private void postFile(){
        try{
        String postReceiverUrl = "https://openshift.redhat.com/app/console/application/537e9c0be0b8cd34170000b6-alldata";

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(postReceiverUrl);

    File file = new File(detail);
    FileBody fileBody = new FileBody(file);

    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("file", fileBody);
    httpPost.setEntity(reqEntity);


        HttpResponse  response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if(resEntity != null){
            String responseStr = EntityUtils.toString(resEntity).trim();
            Log.v("MyApp", "Response:" + responseStr);
        }
        else Log.v("MyApp", "couldn't commnicate with the server");

    }catch (NullPointerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

        }

It throws an exception when it executes the following line:

HttpResponse  response = httpClient.execute(httpPost);

this is the logcat message:

05-27 15:32:01.557: D/libEGL(18546): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
05-27 15:32:01.607: D/libEGL(18546): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
05-27 15:32:01.627: D/libEGL(18546): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
05-27 15:32:01.697: D/OpenGLRenderer(18546): Enabling debug mode 0
05-27 15:32:06.717: I/dalvikvm(18546): Total arena pages for JIT: 11
05-27 15:32:06.717: I/dalvikvm(18546): Total arena pages for JIT: 12
05-27 15:32:06.727: D/dalvikvm(18546): Rejecting registerization due to and-int/lit16 v0, v5, (#128)
05-27 15:32:06.737: D/dalvikvm(18546): Rejecting registerization due to and-int/lit16 v0, v5, (#128)
05-27 15:32:06.817: D/dalvikvm(18546): Rejecting registerization due to move v5, v4, (#0)
05-27 15:32:06.827: D/dalvikvm(18546): Rejecting registerization due to move v5, v4, (#0)
05-27 15:32:06.827: D/dalvikvm(18546): Rejecting registerization due to mul-int/lit8 v4, v2, (#31)
05-27 15:32:06.827: D/dalvikvm(18546): Rejecting registerization due to mul-int/lit8 v4, v2, (#31)
05-27 15:32:06.827: D/dalvikvm(18546): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueParser;.INSTANCE
05-27 15:32:06.837: W/dalvikvm(18546): VFY: unable to resolve static field 3758 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;
05-27 15:32:06.837: D/dalvikvm(18546): VFY: replacing opcode 0x62 at 0x001b
05-27 15:32:06.837: D/dalvikvm(18546): DexOpt: couldn't find static field Lorg/apache/http/message/BasicHeaderValueFormatter;.INSTANCE
05-27 15:32:06.837: W/dalvikvm(18546): VFY: unable to resolve static field 3752 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueFormatter;
05-27 15:32:06.837: D/dalvikvm(18546): VFY: replacing opcode 0x62 at 0x0015
05-27 15:32:06.857: W/System.err(18546): android.os.NetworkOnMainThreadException
05-27 15:32:06.867: W/System.err(18546):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)
05-27 15:32:06.877: W/System.err(18546):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-27 15:32:06.877: W/System.err(18546):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-27 15:32:06.877: W/System.err(18546):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-27 15:32:06.877: D/dalvikvm(18546): Rejecting registerization due to +iget-quick v3, v5, (#20)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-27 15:32:06.877: W/System.err(18546):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-27 15:32:06.877: W/System.err(18546):    at com.measuredistancewalked.PostData.postFile(PostData.java:108)
05-27 15:32:06.877: W/System.err(18546):    at com.measuredistancewalked.PostData.writeFile(PostData.java:82)
05-27 15:32:06.877: W/System.err(18546):    at com.measuredistancewalked.PostData.saveAllData(PostData.java:58)
05-27 15:32:06.877: W/System.err(18546):    at com.measuredistancewalked.DitanceWalkedActivity$1.onClick(DitanceWalkedActivity.java:74)
05-27 15:32:06.877: W/System.err(18546):    at android.view.View.performClick(View.java:4383)
05-27 15:32:06.877: W/System.err(18546):    at android.view.View$PerformClick.run(View.java:18097)
05-27 15:32:06.877: W/System.err(18546):    at android.os.Handler.handleCallback(Handler.java:725)
05-27 15:32:06.877: W/System.err(18546):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 15:32:06.877: W/System.err(18546):    at android.os.Looper.loop(Looper.java:176)
05-27 15:32:06.877: W/System.err(18546):    at android.app.ActivityThread.main(ActivityThread.java:5279)
05-27 15:32:06.877: W/System.err(18546):    at java.lang.reflect.Method.invokeNative(Native Method)
05-27 15:32:06.877: W/System.err(18546):    at java.lang.reflect.Method.invoke(Method.java:511)
05-27 15:32:06.877: W/System.err(18546):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-27 15:32:06.877: W/System.err(18546):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-27 15:32:06.877: W/System.err(18546):    at dalvik.system.NativeStart.main(Native Method)
05-27 15:32:06.887: D/dalvikvm(18546): Rejecting registerization due to +iget-object-quick v7, v9, (#8)
05-27 15:32:06.907: I/dalvikvm(18546): Total arena pages for JIT: 13
05-27 15:32:06.907: I/dalvikvm(18546): Total arena pages for JIT: 14

and I have the following post_file.php code :

<?php
if($_FILES){
$file = $_FILES['file'];
$fileContents =  file_get_contents($file["all_data_file"]);
print_r($fileContents);
}
?>

my question is :

where do i upload my php code? and

what else should i do to communicate with the server other than providing my openshift application url in my code?

Thank you for your help in advance!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • duanang58939 2014-05-27 13:51
    最佳回答

    If your app crashes always take a look at the logcat. In 99% of all cases the error message tells you exactly what's wrong and how you can fix it, just like in your case:

    05-27 15:32:06.857: W/System.err(18546): android.os.NetworkOnMainThreadException
    

    You cannot perform complex tasks in the UI Thread, doing so would cause your app to freeze and lag. In Android there is an extra safe guard against this when it comes to networking. If you try to perform network operations on the UI thread an Exception is thrown, namely this NetworkOnMainThreadException.

    Solution: You have to perform your work in a separate Thread. I recommend you use an AsyncTask for this.
    Take a look at this example for an AsyncTask, the method doInBackground() is executed in a separate Thread, you can perform all your work there. After doInBackground() has finished the result returned by it will be passed to onPostExecute(). onPostExecute() will again be executed on the UI Thread, so here you can return results from the AsyncTask to the UI.

    private class ExampleTask extends AsyncTask<Void, Void, Boolean> {
    
        @Override
        protected Boolean doInBackground(Void... params) {
    
            // Do your work here
    
            // Return true if successful otherwise false
            return true;
        }
    
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
    
            if(result) {
                // Work has been completed successfully    
            } else {
                // Work was not completed successfully
            }
        }
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题