2015-11-02 15 views
1

我可以使用这种方式使用Okhttp将字符串上传到服务器吗?使用okhttp发送数据而没有异步任务

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    usernam = (EditText) findViewById(R.id.username); 
    passw = (EditText) findViewById(R.id.password); 
    email = (EditText) findViewById(R.id.email); 

    name = usernam.getText().toString(); 
    pass = passw.getText().toString(); 
    emails = email.getText().toString(); 

} 


private final OkHttpClient client = new OkHttpClient(); 

public void run() throws Exception { 
    String json = ""; 

    JSONObject jsonObject = new JSONObject(); 
    jsonObject.accumulate("name",name); 
    jsonObject.accumulate("password",pass); 
    jsonObject.accumulate("email",emails); 

    json = jsonObject.toString(); 

    RequestBody formBody = new FormEncodingBuilder() 
      .add("result", json) 
      .build(); 
    Request request = new Request.Builder() 
      .url("https://justedhak.comlu.com/receiver.php") 
      .post(formBody) 
      .build(); 

    Response response = client.newCall(request).execute(); 
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); 

    System.out.println(response.body().string()); 
} 
+0

可能包括与反应式编程,像RX-java的 –

回答

2

如果您想使用没有AsyncTask的OkHttp,可以参考以下示例代码,希望这有助于您!

public class MainActivity extends AppCompatActivity { 
    private static final String LOG_TAG = "OkHttp"; 
    private TextView mTextView; 
    private Handler mHandler; 
    private String mMessage; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mTextView = (TextView) findViewById(R.id.textView); 
     mHandler = new Handler(Looper.getMainLooper()); 
     OkHttpClient client = new OkHttpClient(); 
     // GET request 
     Request request = new Request.Builder() 
       .url("http://...") 
       .build(); 
     client.newCall(request).enqueue(new Callback() { 
      @Override 
      public void onFailure(Request request, IOException e) { 
       mMessage = e.toString(); 
       Log.e(LOG_TAG, mMessage); // no need inside run() 
       mHandler.post(new Runnable() { 
        @Override 
        public void run() { 
         mTextView.setText(mMessage); // must be inside run() 
        } 
       }); 
      } 

      @Override 
      public void onResponse(Response response) throws IOException { 
       mMessage = response.toString(); 
       Log.i(LOG_TAG, mMessage); // no need inside run() 
       mHandler.post(new Runnable() { 
        @Override 
        public void run() { 
         mTextView.setText(mMessage); // must be inside run() 
        } 
       }); 
      } 
     }); 
    } 
} 
+0

嘿男人谢谢你的回答,在这段代码中,你究竟是什么东西?如果我想从数据库中检索specefic值,该怎么办?我们应该添加一个Body?,以及如果我想将某些内容发送到数据库呢?我使用inputsream还是什么? – Moudiz

+0

响应与同步方式相同,如果要发送数据,请使用POST请求 – BNK

+0

谢谢man ill试试 – Moudiz

1

网络调用必须完成关闭的UI线程,所以没有。

你也从来没有打电话给你的run()方法,在您的活动,所以它不会被执行

编辑:最大皮诺是在RXJava正确可以用来使这种调用很容易,但是,仍然会在不同的线程上执行呼叫

+0

我从来没有使用RX java中,要使用到使用异步任务我的方法,顺便说一句是正确的添加JSON这样的代码? .add(“result”,json) – Moudiz

+0

我不确定您的JSON问题,但继承人如何在需要异步任务的自动正确情况下使用RX,如果您感兴趣: http://stackoverflow.com/问题/ 33415151/asynctask-replacement-with-rxjava-help-needed/33421148#33421148 如果它对你有帮助,请将我的答案标记为正确! –

+0

比你的帮助,生病请求另一个问题与我的JSON有关,你可以请它吗?这是网址http://stackoverflow.com/questions/33486899/json-object-and-okhttp-sending-data – Moudiz