扫描后,我试图在字符串中获取结果值。但它不会工作。
我已经想出了如何把它放在一个可以工作的 toast(maketext) 中,但是一个我想不出来的字符串。这是代码,请帮助我。
cpackage com.example.stage.absa;
import android.Manifest;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.Result;
import java.io.IOException;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import android.net.Uri;
import java.net.URL;
public class MainActivity extends AbsRuntimePermission {
String resultCode;
private Button getBtn;
private TextView result;
private OkHttpClient client;
boolean doubleTap = false;
private ZXingScannerView scannerView;
private static final int REQUEST_PERMISSION = 10;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
getBtn = (Button) findViewById(R.id.getBtn);
getBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getWebService();
}
});
client = new OkHttpClient();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
requestAppPermissions(new String[]{
Manifest.permission.CAMERA},
R.string.msg, REQUEST_PERMISSION);
}
@Override
public void onBackPressed() {
if (doubleTap) {
super.onBackPressed();
} else {
Toast.makeText(this, "Druk Terug opnieuw om de app te sluiten", Toast.LENGTH_SHORT).show();
doubleTap = true;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
doubleTap = false;
}
}, 500);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void browser1(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
startActivity(browserIntent);
}
public void scanCode(View view) {
scannerView = new ZXingScannerView(this);
scannerView.setResultHandler(new ZxingScannerResultHandler());
setContentView(scannerView);
scannerView.startCamera();
}
class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
@Override
public void handleResult(Result result) {
// String resultCode = result.getText();
// Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();
resultCode = result.getText();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scannerView.stopCamera();
}
}
private void getWebService() {
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.appendPath(getString(R.string.ipadres))
.appendPath(getString(R.string.poort))
.authority("barcode/")
.appendPath(getString(R.id.))
String URL = builder.build().toString();
final Request request = new Request.Builder().url("http://192.168.17.12:9083/barcode/8712345501006").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
result.setText("Mislukt");
}
});
}
@Override
public void onResponse(Call call, final Response response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
result.setText(response.body().string());
} catch (IOException ioe) {
result.setText("Error tijdens verkrijgen body");
}
}
});
}
});
}
@Override
public void onPermissionsGranted(int requestCode) {
//Do anything when permisson granted
Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
}
}