以编程方式在android中通过wifi连接发送和接收数据

时间:2015-06-11 08:09:36

标签: java android android-wifi

我正在尝试创建一个与我的电脑中的热点通信的应用。我能够连接到热点,但我无法弄清楚如何在android中发送和接收数据。这是我现在在我的应用程序中的代码。

 - (void)parseXMLFileAtURL:(NSString *)URL
{

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{


    currentElement = [elementName copy];
    if ([elementName isEqualToString:@"item"]) {
        item = [[NSMutableDictionary alloc] init];
        currentTitle = [[NSMutableString alloc] init];
        currentDate = [[NSMutableString alloc] init];
        currentSummary = [[NSMutableString alloc] init];
        currentLink = [[NSMutableString alloc] init];



    }if ([elementName isEqualToString:@"enclosure"]){
        NSString *imageStr = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
        [item setObject:imageStr forKey:@"_url"];
    }

    if ([elementName isEqualToString:@"media:thumbnail"]) {
        [item setObject:currentPhoto forKey:@"_url"];}

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    //NSLog(@"ended element: %@", elementName);
    if ([elementName isEqualToString:@"item"]) {
        [item setObject:currentTitle forKey:@"title"];
        [item setObject:currentLink forKey:@"link"];
        [item setObject:currentSummary forKey:@"description"];
        [item setObject:currentDate forKey:@"pubDate"];
        [item setObject:currentLogo forKey:@"logo"];


        if (itemCount<5) {

        [storiesNationale addObject:[item copy]];
        itemCount++;
    }
        NSLog(@"adding story: %@", currentTitle);


    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...
    if ([currentElement isEqualToString:@"title"]) {
        [currentTitle appendString:string];

    } else if ([currentElement isEqualToString:@"link"]) {
        [currentLink appendString:string];

    } else if ([currentElement isEqualToString:@"description"]) {
        [currentSummary appendString:string];

    } else if ([currentElement isEqualToString:@"pubDate"]) {
        [currentDate appendString:string];
    }



}

我已经查看了其他类似的问题,但似乎没有任何内容与我的应用程序一起使用。

1 个答案:

答案 0 :(得分:0)

通过执行上述操作,您只能连接到计算机的热点(Windows PC - 假设您使用的是netsh wlan)。这将允许您连接到您的PC并最大限度地分享其互联网。但要启用计算机和手机之间的通信,您需要在此层上构建应用程序。

这款应用的功能是什么。它将侦听您的PC和移动设备上的特定端口。因此,当建立通过热点的连接时,您需要Socket并执行Socket.connect(new InetAddress(IP, PORT))。假设您的计算机在PORT_0上收听,那么您的移动设备需要Socket.connect(new InetAddress(IP_COMPUTER, PORT_0))

通过这种方式,您现在可以使用一个插槽,可用于PC和移动设备之间的通信。