https://paste.ubuntu.com/24800939/
-----------------------------
package com.example.argede_8.wsclient;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.gson.JsonParser;
import org.java_websocket.client.WebSocketClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
public class MainActivity extends AppCompatActivity {
private OkHttpClient okHttpClient;
private final class EchoWebSocketListener extends WebSocketListener{
private static final int Normal_Clouruse_Status = 1000;
@Override public void onOpen(okhttp3.WebSocket webSocket, Response response) {
Log.e("asdasd","onopen");
}
@Override public void onMessage(WebSocket webSocket, ByteString bytes) {
Log.e("Geldi On Mesage =", String.valueOf(bytes));
//JSONObject jsonObject = new JSONObject((Map) bytes); //JsonParseObdData(jsonObject); }
@Override public void onMessage(WebSocket webSocket, String text) {
Log.e("OnMessage =",text);
text.replace("","|wroomTXT|");
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(text);
} catch (JSONException e) {
Log.e("jsonExcept = ",e.toString());
}
JsonParseObdData(jsonObject);
}
@Override public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(Normal_Clouruse_Status,null);
Log.e("OnClosing","code ="+ String.valueOf(code) +" reason ="+reason);
Dinle();
}
@Override public void onFailure(WebSocket webSocket, Throwable t, Response response) {
Log.e("OnError",t.toString());
Dinle();
}
}
private TextView a,b,c,d,e,f;
private Button btn_dinle;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okHttpClient = new OkHttpClient();
a = (TextView)findViewById(R.id.a);
b = (TextView)findViewById(R.id.b);
c =(TextView)findViewById(R.id.c);
d =(TextView)findViewById(R.id.d);
e = (TextView)findViewById(R.id.e);
f = (TextView)findViewById(R.id.f);
btn_dinle = (Button)findViewById(R.id.btn_dinle);
btn_dinle.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
Dinle();
}
});
}
public void Dinle(){
Request request = new Request.Builder().url("ws://192.168.43.239:80").build();
EchoWebSocketListener listener = new EchoWebSocketListener();
WebSocket ws = okHttpClient.newWebSocket(request,listener);
// okHttpClient.dispatcher().executorService().shutdown(); }
private void JsonParseObdData(JSONObject data){
JSONObject jsonObject = (JSONObject) data;
final Iot_Arac_OBD_Model model = new Iot_Arac_OBD_Model();
try {
model.seta(jsonObject.getString("a").toString());
model.setb(jsonObject.getString("b").toString());
model.setc(jsonObject.getString("c").toString());
model.setd(jsonObject.getString("d").toString());
model.sete(jsonObject.getString("e").toString());
model.setf(jsonObject.getString("f").toString());
} catch (JSONException e) {
Log.e("JsonparseHata =",e.toString());
}
runOnUiThread(new Runnable() {
public void run(){
a.setText("(1)"+model.geta());
b.setText("(2)"+model.getb());
c.setText("(3)"+model.getc());
d.setText("(4)"+model.getd());
e.setText("(5)"+model.gete());
f.setText("(6)"+model.getf());
}
});
}
}
----------------------------------------
package com.example.argede_8.wsclient;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/** * Created by ARGEDE_8 on 6/7/2017. */
public class Iot_Arac_OBD_Model {
@SerializedName("a")
@Expose private String a;
@SerializedName("b")
@Expose private String b;
@SerializedName("c")
@Expose private String c;
@SerializedName("d")
@Expose private String d;
@SerializedName("e")
@Expose private String e;
@SerializedName("f")
@Expose private String f;
public void seta(String a){
this.a = a;
}
public String geta(){
return this.a;
}
public void setb(String b){
this.b = b;
}
public String getb(){
return this.b;
}
public void setc(String c){
this.c = c;
}
public String getc(){
return this.c;
}
public void setd(String d){
this.d = d;
}
public String getd(){
return this.d;
}
public void sete(String e){
this.e = e;
}
public String gete(){
return this.e;
}
public void setf(String f){
this.f = f;
}
public String getf(){
return this.f;
}
}
-------------------
https://paste.ubuntu.com/24800942/
Hiç yorum yok:
Yorum Gönder